Jump to content

Help with server and client in single thread?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
liamzebedee

liamzebedee

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
I'm developing my game and are developing singleplayer and multiplayer modes. Based on most modern games (excluding Minecraft) I am developing single player as a local loopback server to save time developing.

I'm having trouble with my server code. Everytime I compile and execute the application hangs then crashes (not responds). I'm using the Slick2d game library which is single threaded. Here is my code-

package chill.util;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.*;


public class ChillServer {


	int port;

	String name = "[ChillServer]";

	

	 String fromclient;

     String toclient;

	

	

	public ChillServer(int portNumber) throws IOException

	{

		this.port = portNumber;

		ServerSocket Server = new ServerSocket (port);

		out("ChillServer established on port "+port);

		while(true) 

        {

			out("Listening for connections...");

        	Socket connected = Server.accept();

           System.out.println( " THE CLIENT"+" "+

           connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");

           

           BufferedReader inFromUser = 

           new BufferedReader(new InputStreamReader(System.in));    

    

           BufferedReader inFromClient =

              new BufferedReader(new InputStreamReader (connected.getInputStream()));

                 

           PrintWriter outToClient =

              new PrintWriter(

                 connected.getOutputStream(),true);

           

           while ( true )

           {

           	

           	System.out.println("SEND(Type Q or q to Quit):");

           	toclient = inFromUser.readLine();

           	

           	if ( toclient.equals ("q") || toclient.equals("Q") )

           	{

           		outToClient.println(toclient);

           		connected.close();

           		break;

           	}

           	else

           	{

           	outToClient.println(toclient);

               }

           	

           	fromclient = inFromClient.readLine();

           	

               if ( fromclient.equals("q") || fromclient.equals("Q") )

               {

               	connected.close();

               	break;

               }

               	

		        else

		        {

		         System.out.println( "RECIEVED:" + fromclient );

		        } 

			    

			}  

			

         }

	}

	public void out(String s)

	{

		System.out.println(name+" "+s);

	}

	

}



I am having the game crash in my init method when I construct it-
ChillServer server = new ChillServer(3377);

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Please post stacktraces when there's an error.

#3
liamzebedee

liamzebedee

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
Never mind, it was todo with the issue of Slick being single threaded and running a server would cause the program to hang on listening for connections. By making the Server class a Thread it ran fine.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users