The application should always be listening for connections.
Your serverSocket.accept() method call should be in a while loop like this:
// server infinite loop
import java.net.*;
import java.io.*;
public class tcpServer
public static void main(String args[])
int port;
ServerSocket server_socket;
BufferedReader input;
try
port = Integer.parseInt(args[0]);
catch (Exception e)
System.out.println("port = 1500 (default)");
port = 1500;
try
server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " +
server_socket.getLocalPort());
while(true)
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
// print received data
try
while(true)
String message = input.readLine();
if (message==null) break;
System.out.println(message);
catch (IOException e)
System.out.println(e);
// connection closed by client
try
socket.close();
System.out.println("Connection closed by client");
catch (IOException e)
System.out.println(e);
The accept method will block until a connection is made to the server
socket port. Once a socket is established, you need to do whatever you
need to do to process the data going into or out of the socket by getting
This message was posted using eunum
To interact with a real-time, threaded interface to this e-mail list, clickthe link below:
[EMAIL PROTECTED]
