# Problem beim Datenempfang



## mavinatic (14. Okt 2011)

Hallo Community,

ich bin dabei ein kleines Multiplayerspiel zu programmieren, jedoch habe nun etwas den Code überarbeitet und muss feststellen, wenn ich etwas von einem client sende, empfängt mein Server nichts, obwohl er in einer while(true) schleife hängt:

Mein Server(ausschnitt, wo er Daten empfängt):

threadRunning ist IMMER true, ich habe noch nichts programmiert, was den Zustand ändert.

```
@Override
	public void run()
	{
		while(threadRunning)
		{
			try 
			{
				if(!clientSocket.isConnected())
				{
					if(reader!=null)
					{
						String nextLine = reader.readLine();
						
						/*
						 * Test
						 */
						System.out.println(nextLine);
						
						String[] protocolParts = nextLine.split(" ");
						if(protocolParts[2]!=null)
						{
							if(protocolParts[1]!=null)
							{
								processCommand(protocolParts[1]);
							}
						}
					}	
				} else
				{
					threadRunning = false;
					reader.close();
					writer.close();
					clientSocket.close();
				}
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
```

Mein SimpleClient:

```
public class TestClient {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, IOException {
		// TODO Auto-generated method stub
		Socket socket = new Socket("localhost",1234);
		PrintWriter writer = new PrintWriter(socket.getOutputStream());
		BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		writer.println("GET /commonRequest URLGS/1.0");
		writer.flush();
//		while(true)
//		{
//			System.out.println(reader.readLine());
//		}	
		writer.close();
		reader.close();
		socket.close();
	}

}
```


----------



## Tomate_Salat (14. Okt 2011)

Gibts fehler?
Ist alles korrekt initialisiert?
Wie/wo und wann erstellst du den reader?


----------



## mavinatic (14. Okt 2011)

```
package com.urlserver.customizing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.urlserver.server.AbstractConnection;
import com.urlserver.server.RequestException;
import com.urlserver.server.URLServer;

public class ClientConnection extends AbstractConnection {
	private URLServer server = null;
	private PrintWriter writer = null;
	private BufferedReader reader = null;
	private Socket clientSocket = null;
	
	private boolean threadRunning = true;
	
	private final String PLAYERNAME = "PLAYERNAME";
	private final String POSX = "POSX";
	private final String POSY = "POSY";
	
	public ClientConnection(URLServer server, Socket socket) 
	{
		try
		{
			this.server = server;
			this.clientSocket = socket;
			this.writer = new PrintWriter(socket.getOutputStream());
			this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
						
			this.start();
		} catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void send(String strMessage) 
	{
		if(writer!=null)
		{
			writer.println(strMessage);
			writer.flush();
		}
	}

	@Override
	public void processCommand(String strMessage) 
	{
		//GET /status URLGS/1.0
		if(strMessage.contains(URLGSRequests.STATUS_REQUEST))
		{
			String xml = server.getStatusAsXML();
			send(xml);
		}
		//GET /initRequest URLGS/1.0
		if(strMessage.contains(URLGSRequests.INIT_REQUEST))
		{
			Player[] players = server.getRepository().getPlayers();
			String xml = "<?xml version=\"1.0\"?><RESPONSE TYPE=\"INIT\"><PLAYERS>";
			for(Player currentPlayer:players)
			{
				xml += "<PLAYER>" +
						"<PLAYERNAME>"+currentPlayer.getPlayerName()+"</PLAYERNAME>" +
						"<X>"+currentPlayer.getX()+"</X>" +
						"<Y>"+currentPlayer.getY()+"</Y>" +
						"<IMAGE>"+currentPlayer.getCurrentImageName()+"</IMAGE>" +
						"</PLAYER>";
			}
			xml += "</PLAYERS></RESPONSE>";
			
			send(xml);
		}
		//GET /commonRequest URLGS/1.0
		if(strMessage.contains(URLGSRequests.COMMON_REQUEST))
		{
			Player[] players = server.getRepository().getPlayers();
			String xml = "<?xml version=\"1.0\"?><RESPONSE TYPE=\"COMMON\"><PLAYERS>";
			for(Player currentPlayer:players)
			{
				xml += "<PLAYER>" +
						"<PLAYERNAME>"+currentPlayer.getPlayerName()+"</PLAYERNAME>" +
						"<X>"+currentPlayer.getX()+"</X>" +
						"<Y>"+currentPlayer.getY()+"</Y>" +
						"<IMAGE>"+currentPlayer.getCurrentImageName()+"</IMAGE>" +
						"</PLAYER>";
			}
			xml += "</PLAYERS></RESPONSE>";
			
			send(xml);
		}
		//POST /addPlayer?playerName=Testplayer&posX=15&posY=20 URLGS/1.0
		if(strMessage.contains(URLGSRequests.ADD_PLAYER))
		{
			String[] questionMarkSplit = strMessage.split("?");
			if(questionMarkSplit[1]!=null)
			{
				String playerName = new String();
				int posX = 0;
				int posY = 0;
				
				String[] params = questionMarkSplit[1].split("&");
				for(int i=0;i<params.length;i++)
				{
					String[] keyValuePair = params[i].split("=");
					if(keyValuePair[0].toUpperCase().equals(PLAYERNAME))
					{
						playerName=keyValuePair[1];
					}
					if(keyValuePair[0].toUpperCase().equals(POSX))
					{
						posX=Integer.parseInt(keyValuePair[1]);
					}
					if(keyValuePair[0].toUpperCase().equals(POSY))
					{
						posY=Integer.parseInt(keyValuePair[1]);
					}
				}
				
				Player player = new Player();
				player.setPlayerName(playerName);
				player.setX(posX);
				player.setY(posY);
				
				String xml = "";
				try {
					server.getRepository().createPlayer(player);
					xml = "<?xml version=\"1.0\"?><RESPONSE TYPE=\"COMMIT\">SUCCESS</RESPONSE>";
				} catch (DataRepositoryException e) {
					xml = "<?xml version=\"1.0\"?><RESPONSE TYPE=\"COMMIT\">EXCEPTION:"+e.getMessage()+"</RESPONSE>";
				}
							
				send(xml);
			}
		}
		//POST /positionRefresh?playerName=Testplayer&posX=12&posY=99 URLGS/1.0
		if(strMessage.contains(URLGSRequests.POSITION_REFRESH))
		{
			String[] questionMarkSplit = strMessage.split("?");
			if(questionMarkSplit[1]!=null)
			{
				String playerName = "";
				int posX = 0;
				int posY = 0;
				
				String[] params = questionMarkSplit[1].split("&");
				for(String param:params)
				{
					String[] keyValuePair = param.split("=");
					if(keyValuePair[0].toUpperCase().equals(PLAYERNAME))
					{
						playerName = keyValuePair[1];
					}
					if(keyValuePair[0].toUpperCase().equals(POSX))
					{
						posX = Integer.parseInt(keyValuePair[1]);
					}
					if(keyValuePair[0].toUpperCase().equals(POSY))
					{
						posY = Integer.parseInt(keyValuePair[1]);
					}
				}
				
				Player player = server.getRepository().getPlayer(playerName);
				if(player!=null)
				{
					player.setX(posX);
					player.setY(posY);
				} else
				{
					throw new RequestException(RequestException.REQUEST_ERROR_NO_PLAYER_FOUND+URLGSRequests.POSITION_REFRESH);
				}
			}
		}
		//POST /removePlayer?playerName=Testplayer URLGS/1.0
		if(strMessage.contains(URLGSRequests.REMOVE_PLAYER))
		{
			String[] questionMarkSplit = strMessage.split("?");
			if(questionMarkSplit[1]!=null)
			{
				String playerName = "";
				String[] params = questionMarkSplit[1].split("&");
				for(String param:params)
				{
					String[] keyValuePair = param.split("=");
					if(keyValuePair[0].toUpperCase().equals(PLAYERNAME))
					{
						playerName = keyValuePair[1];
					}
				}
				
				Player player = server.getRepository().getPlayer(playerName);
				if(player!=null)
				{
					String xml = "";
					try {
						server.getRepository().deletePlayer(player);
						xml = "<?xml version=\"1.0\"?><RESPONSE TYPE=\"COMMIT\">SUCCESS</RESPONSE>";
					} catch (DataRepositoryException e) {
						xml = "<?xml version=\"1.0\"?><RESPONSE TYPE=\"COMMIT\">EXCEPTION:"+e.getMessage()+"</RESPONSE>";
					}
				} else 
				{
					throw new RequestException(RequestException.REQUEST_ERROR_NO_PLAYER_FOUND+URLGSRequests.REMOVE_PLAYER);
				}
			}
		}
	}

	@Override
	public void run()
	{
		while(threadRunning)
		{
			try 
			{
				if(!clientSocket.isConnected())
				{
					if(reader!=null)
					{
						String nextLine = reader.readLine();
						
						/*
						 * Test Purposes
						 */
						System.out.println(nextLine);
						
						/*
						 * URLGS Protocol Validation
						 */
						String[] protocolParts = nextLine.split(" ");
						if(protocolParts[2]!=null)
						{
							if(protocolParts[1]!=null)
							{
								processCommand(protocolParts[1]);
							}
						}
					}	
				} else
				{
					threadRunning = false;
					reader.close();
					writer.close();
					clientSocket.close();
				}
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}
```

Bei jedem Verbinden, werden die Daten Objekte übergeben im Konstruktor. Fehler gibt es keine, es kommt lediglich nichts an. Initialisiert ist alles korrekt, meiner Meinung nach.


In der Mainclass:

```
public void start()
	{
		try
		{
			serverSocket = new ServerSocket(serverPort);
			
			System.out.println(welcomeMessage);
			
			while(true)
			{
				Socket client = serverSocket.accept();
				
				System.out.println("Client connected: "+client.getInetAddress().getHostAddress());
				
				AbstractConnection aConnection = new ClientConnection(this, client);
			}
			
		} catch(Exception e)
		{
			e.printStackTrace();
		}
	}
```


----------

