Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ServerSocket.accept(); problem  RSS feed

 
Daniel Silva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm try to run this code exemple and the program break after the comand ServerSocket srvr = new ServerSocket(1234), the message "object created..." is printed but the message "accepted..." not what should be wrong ???
import java.lang.*;
import java.io.*;
import java.net.*;
class Server {
public static void main(String args[]) {
String data = "Toobie ornaught toobie";
try {
ServerSocket srvr = new ServerSocket(1234);
System.out.println("object created...");
Socket skt = srvr.accept();
System.out.println("accepted...");
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
System.out.print("Sending string: '" + data + "'\n");
out.print(data);
out.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you expect accept to do? And when? The answer I'm fishing for is that it will respond to a client program attempting to connect to the open ServerSocket. Do you have a client program, too? Accept will wait forever (see JavaDoc ... is there a time out?) for a client to connect.
With most IDEs you can start a program, then select and start another program. You will probably be able to run the client and server programs right in the IDE and debug step through both to see how they interact.
 
derek_ho
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program is block on trying to accept new client. Is your client program connecting to the correct server IP and server port?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!