Here is an example:
Code:
public class Server implements Runnable{
// Port on which your serverSocket will accept the connection
private final int port=3333;
// Server socket
private ServerSocket serverSocket;
// thread
private Thread thServer;
//variable to stop your thread (yad alternative)
private boolean inPrivate=false;
//Constructor
public Server(){
serverSocket=new ServerSocket(port);//instantiates the server socket
thServer=new Thread(this,"My Server");//create the thread
thServer.start();//starts the thread's run
}
//run the thread
public void run(){
inPrivate=true;
while(inPrivate){
serverSocket.accept();
}
}
}
But that does not do much except accept the connection. To you create a class to use your socket by replacing:
Code:
serverSocket. accept ( ) ;
by
Code:
new Reception (server.accept());
Code:
public class Reception{
BufferedInputStream bis;
char read;
public Reception(Socket mySocket){
bis=new BufferedInputStream (mySocket.getInputStream());
while((read=bis.read())!=-1){
System.out.println(read);
}
}
}
Well it must miss the block try() catch() {}, must also pass reception in thread. Otherwise you will have concerns for a more connected to your server
Bookmarks