Results 1 to 16 of 16
Thread: Problem with Sockets
- 09-05-2010, 06:06 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Problem with Sockets
hello,
I'm having problems with this server socket class (multi threading) and I don't know how to solve it.
I'm able to connect from a client and send a messege to this server and it does what I want wich is put it into a list and then write it on a Jpanel. The problem is when I send a second message from client to server, is simply doesn't work.
Can anybody help me, pls?
Here's the code:
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; public class Server extends Thread{ boolean active = true; int portServidor = 54321; BufferedReader in; placard placa; boolean Istrue = true; String input; PrintWriter outt = null; ServerWorker sworker = new ServerWorker(); public Server(placard pub) { super("ServerThread"); placa = new placard(); //placa = pub; placa.setVisible(true); } public void run(){ while (true) { try { ServerSocket server = new ServerSocket(portServidor); System.out.println("Listening..."); while(active){ Socket socket = server.accept(); String ip = String.valueOf(socket.getInetAddress()); //writes into the Jpanel the cleint's IP placa.novaLigacao(""); placa.novaLigacao(ip); in = new BufferedReader( new InputStreamReader( socket.getInputStream())); outt = new PrintWriter(socket.getOutputStream(), true); input = in.readLine(); //do a check to the prefix of message to choose what to do next - put in the list or remove from the list sworker.doOperacao(input); //writes next message from the list into the "wall" placa.escreve(sworker.getMensagem()); //in.close(); //outt.close(); } //socket.close(); } catch (IOException e) { System.out.println(e); } System.out.println("Exit"); } } public static void main(String[] args) throws IOException { new Server(null).start(); } }Last edited by Fubarable; 09-05-2010 at 06:40 PM. Reason: Moderator Edit: Code tags added
-
Not sure if this is your problem, but where do you create and run separate threads for each client recognized by the server?
- 09-05-2010, 07:43 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
I'm not sure if I understand your question, but a new thread is started when I run this class (it's not a real world program).
-
No, that's not what I meant. If this is truly a sever that services multiple clients, real-world or not, it must create a new thread each time the server connects with a new client.
Again, I'm no pro at this, far from it, so corrections are most welcome, but for instance,
Java Code:class MyServer { public static final int PORT_NUMBER = 4442; private ServerSocket serverSocket; private boolean keepGoing = true; public MyServer() { try { // start the server listening serverSocket = new ServerSocket(PORT_NUMBER); } catch (IOException e) { System.err.println("Could not listen on port: " + PORT_NUMBER + "."); System.exit(1); } while (keepGoing) { Socket clientSocket = null; try { // if connects to a client clientSocket = serverSocket.accept(); // create a runnable that services the client MyServerRunnable myServRunnable = new MyServerRunnable(clientSocket); // and run this in its own thread new Thread(myServRunnable).start(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } } if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } // a Runnable to allow the server to service the client class MyServerRunnable implements Runnable { private Socket clientSocket; private PrintWriter out; private BufferedReader in; private String inputLine, outputLine; public MyServerRunnable(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { try { out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); long time = System.currentTimeMillis(); while ((inputLine = in.readLine()) != null) { // get input from client and send out any output... } } catch (IOException e) { e.printStackTrace(); } try { if (out != null) { out.close(); } if (in != null) { in.close(); } if (clientSocket != null) { clientSocket.close(); } } catch (IOException e) { e.printStackTrace(); } } }
For more on this, please check out the Oracle IO tutorial, and in particular, the section on "Supporting Multiple Clients"
Luck!
-
Sorry, (if this is a single client app) I'm wrong. One of your problems I believe is that your server has no while loop with which to service the client; this portion in my example above:
Again, the tutorial will explain this to you: Client-Server TutorialJava Code:while ((inputLine = in.readLine()) != null) { // get input from client and send out any output... }
- 09-05-2010, 08:48 PM #6
Where does the server code stop executing? Is it blocked waiting for a connection or ???when I send a second message from client to server, is simply doesn't work.
Does it throw an exception?
- 09-05-2010, 09:21 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
This is ment to serve multiple clients.
There's no Exception.
I think the problems is in this part:
while(active){
Socket socket = server.accept();
With this, it accepts multiple clients but each client only can send one message (the second "server.accept()" doesnt work because there's already a connection).
If I put the server.accept() before the while(active) loop, the client can send as many messages we wants, but of course this prevents other clients to connect.
- 09-05-2010, 09:33 PM #8
Why do you think that statement is the problem?I think the problems is in this part:
have you put print outs before and after that statement to show that is where the server is stopping?
Look at the code Fubarable posted.
Here is the server loop in a server I use for testing:
Java Code:while (true) { // Forever loop Socket s = ss.accept(); // Wait for client if (stopped) { // were we asked to stop? log.log("run(S): stopping"); return; // Early exit if stopped } log.log("run(S): " + ++runS_connects + " connection accepted: " + s + " TO:" + s.getSoTimeout()); hisIP = s.getInetAddress().getHostAddress(); // extract IP addr HandleRequest dr = new HandleRequest(s, httpd.this); // process request } // end while() Forever loop
- 09-05-2010, 10:07 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
-
Let's look at your code. Here the server will connect with a single client:
Java Code:while(active){ // the server here connects with a client Socket socket = server.accept();
And here, your server connects with the client's streams
Java Code:in = new BufferedReader( new InputStreamReader( socket.getInputStream())); outt = new PrintWriter(socket.getOutputStream(), true);
And here you read in a single line from the client's input stream:
But where do you read in the rest of the lines? You need a while ((input = in.readLine()) != null) block here to catch all the lines coming in.Java Code:input = in.readLine();
-
- 09-05-2010, 10:34 PM #12
Fubarable:
Yes, that's correct.
pinheiroa
Assuming that the server code is correct. My and Fubarable's experiences are that it is.
Then are you sure that there is a client trying to connect to the server?
- 09-05-2010, 10:48 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
yap, quite sure.
I tested my client againts your server code example and it works just fine.
I can send more than one message and also use multiple clients. Am I allowed to use part of your code?
I think it solves my problem!
-
Whose code? Mine? or Norms? Mine is derived from the Oracle tutorials.
- 09-05-2010, 10:52 PM #15
My code is worth what you pay for it. It's absolutely free.
-
Similar Threads
-
Problem with sockets reading in defined thread.
By Dodo in forum New To JavaReplies: 6Last Post: 11-18-2009, 11:39 PM -
TCP sockets problem
By duffman.sk in forum NetworkingReplies: 1Last Post: 10-30-2009, 05:06 AM -
Problem (sockets)
By Blacknight962 in forum New To JavaReplies: 3Last Post: 08-16-2009, 12:56 AM -
Sockets and JFrames problem
By marcg11 in forum Advanced JavaReplies: 6Last Post: 08-15-2009, 01:07 PM -
Sockets NIO
By aamp in forum New To JavaReplies: 3Last Post: 01-15-2009, 10:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks