client/server message send problem
For some reason, only the first message from the client gets displayed. Here's the code.
Code:
public static void serverStart(int port) throws IOException {
ServerSocket s = new ServerSocket(port);
System.out.println("Server is now awaiting for incoming connections on port " + port);
Socket client = s.accept();
System.out.println("Client from " + client.getInetAddress() + " connected!");
while (true) {
try {
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
String message = in.readUTF();
System.out.println("Client sent message: " + message);
}
catch (IOException e ) { }
}
}
Code:
public static void clientConnect(String hostname, int port) throws IOException {
while (true) {
try {
Socket s = new Socket(hostname, port);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
System.out.println("Enter a message to send to the server:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String message = stdin.readLine();
out.writeUTF(message);
out.flush();
}
catch (IOException e) { }
}
}
Check the terminal stuff out:
http://img253.imageshack.us/my.php?image=problemqo7.jpg
When i tyed the first message on the client, the server got it. but when i typed the other stuff, server didnt output anything :s
Re: client/server message send problem
You don't need to create streams and a socket each time you want to send a message. If you only accept one socket, then only use one socket.
*On your other messages, the server is not accepting your socket, but they shouldn't be sent with different sockets.
Re: client/server message send problem
Quote:
Originally Posted by System_Error
You don't need to create streams and a socket each time you want to send a message. If you only accept one socket, then only use one socket.
*On your other messages, the server is not accepting your socket, but they shouldn't be sent with different sockets.
hm, im still confused mate. could you show me an example off how to correct it please?
Re: client/server message send problem
Try this (off the top of my head):
Code:
public static void clientConnect(String hostname, int port) throws IOException {
Socket s = new Socket(hostname, port);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
System.out.println("Enter a message to send to the server:");
String message = stdin.readLine();
out.writeUTF(message);
out.flush();
}
catch (IOException e) { }
}
}
out.close();
in.close();
stdin.close();
s.close();
Make sure you are closing the connection when you are done.
Re: client/server message send problem
Thanks alot system Error. How does this look for the server code?
Code:
public static void serverStart(int port) throws IOException {
try {
ServerSocket s = new ServerSocket(port);
System.out.println("Server is now awaiting for incoming connections on port " + port);
Socket client = s.accept();
System.out.println("Client connected!");
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
if (SHARED == false) {
out.writeUTF(SECRET_KEY);
out.flush();
SHARED = true;
}
while (true) {
String message = in.readUTF();
System.out.println("Client sent message: " + message);
System.out.println("Enter a message to send to the client:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String m = stdin.readLine();
out.writeUTF(m);
out.flush();
}
}
catch (IOException e) { }
}
Re: client/server message send problem
I haven't tested it, but it looks good except for one thing... Make sure you close that socket and those streams.
*I mean't to close the streams and sockets at the end of your connect method in my last post. I accidently posted outside of the method.
If you give me full code for your client and server, then I can test it out tommorow.
PS: You may want to come up with some formal way of closing. Make sure the client has some real way of escaping the connection, not just relying on the while loop to end for them.
Hope that makes sense.
Re: client/server message send problem
Thanks alot mate. Sorry, I totally forgot about the stream and socket closing.
Basically, if the client/server types 'exit' then the programs should end. I've yet to add this part.
But for now, I've got it to so that the client/server can send each other messages, with your help.
Is there any chance I may add you to MSN or anything?
Re: client/server message send problem
I don't have MSN, but PM me and we can get another method of communication.