[thelist] Java Sockets

m.j.milicevic me at machak.com
Wed Mar 15 09:08:15 CST 2006


[snip]
lines got mangled up in previous message:

public static void main(String[] args){
// create serversocket ss, surround 
// with try catch or throw exceptions:
// main server lop would look like:
while 
(true){    
new SocketHandlerThread(ss.accept()).start();
}
}
// and your socket handler would be something like:
public class 
SocketHandlerThread extends Thread 
{
private Socket socket = null;     
public 
SocketHandlerThread(Socket socket)  
{  
super("SocketHandlerThread"); 
this.socket = socket;
}    
public void run()  
{ 
try 
{       
//autoflush 
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);     
String input, output; 
out.println("hello");// just echo data:     
while ((input = in.readLine()) != null)   
{   
out.println("you said:");   
out.println(input);   
if 
(input.equals("QUIT")) {  break;   }     
}
//cleanup     
out.close(); 
in.close();   
socket.close(); 
} 
catch (IOException e) 
{     
//deal with 
exceptions 
}    
}
}



kind regards,
-m.j.milicevic
http://www.machak.com




More information about the thelist mailing list