4 Replies - 678 Views - Last Post: 28 September 2015 - 03:58 AM Rate Topic: -----

#1 zceezzh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-January 15

Socket Issue: Message can only be forwarded for one time

Posted 27 September 2015 - 04:22 AM

Hello guys, recently I am attempting an exercise that requires to design a server with TWO clients based on tcp socket. After connected, the SenderClient should send a message typed by user to Server, and Server should then forward that message to the ReceiverClient. Only when user types "quit" in sender, all the programs will be terminated, otherwise they are always listening for messages.
Now I have met the problem that: when I run that three programs in Eclipse Luna, what I found is that the message can be successfully passed from SenderClient -> Server -> ReceiverClient for only one time. And after that, the message will be blocked at the Server. Can you guys also run those three programs on your computer to see the strange phenomenon. Thank you and really need help here.


import java.io.*;
import java.net.*;

public class Server {

	public static void main (String args[]) {
		
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		OutputStream os = null;
		PrintWriter pw = null;
		
		String info = null;
		
		try {
			// listening to port
			ServerSocket serverSocket = new ServerSocket(8888);
			System.out.println("Server is listening to port 8888...");
			while (true) {
				// respond to clients
				Socket receiverSocket = serverSocket.accept();
				System.out.println("receiver client connected!");
				Socket senderSocket = serverSocket.accept();
				System.out.println("sender client connected!");
				// get input stream, read messages from sender
				is = senderSocket.getInputStream();
				isr = new InputStreamReader(is);
				br = new BufferedReader(isr);
				info = br.readLine();
				// close all resources when user types "quit"
				if (info.equalsIgnoreCase("quit")) {
					// close resources when user types "quit"
					is.close();
					isr.close();
					br.close();
					os.close();
					pw.close();
					serverSocket.close();
					System.out.println("Server terminated!");
					break;
				}
				// print out the message
				if (info != null) {
					System.out.println("Sender -> Server: " + info);
				}				
				// get output stream, forward messages to receiver			
				os = receiverSocket.getOutputStream();
				pw = new PrintWriter(os);
				pw.println(info);
				pw.flush();
			} // end while
		} catch (IOException e) {
			e.printStackTrace();
		} // end try...catch
		
	} // end main method
	
} // end class Server





import java.io.*;
import java.net.*;

public class ReceiverClient {

	public static void main (String args[]) {
		
		InputStream is = null;
		BufferedReader br = null;
		
		String info = null;
		
		try {
			while (true) {
				// create receiver socket with host and port number
				Socket receiverSocket = new Socket("localhost", 8888);
				// get input stream, read the information
				is = receiverSocket.getInputStream();
				br = new BufferedReader(new InputStreamReader(is));
				info = br.readLine();
				// close all resources when user types "quit"
				if (info.equalsIgnoreCase("quit")) {
					is.close();
					br.close();
					System.out.println("Receiver client terminated!");
					break;
				}
				// print out the message
				if (info != null) {
					System.out.println("Sender -> Server -> Receiver: " + info);
				}
				receiverSocket.close();
			} // end while
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} // end try...catch
		
	} // end main method
	
} // end class Client



import java.io.*;
import java.net.*;

public class SenderClient {

	public static void main (String args[]) {
		
		OutputStream os = null;
		PrintWriter pw = null;
		BufferedReader br = null;
		
		String userInput = null;
		
		try {
			while (true) {
				// create sender socket with host and port number
				Socket senderSocket = new Socket("localhost", 8888);
				// get message from user input
				System.out.println("Please input a message:");
				br = new BufferedReader(new InputStreamReader(System.in));
				userInput = br.readLine();
				// get output stream, send message to server
				os = senderSocket.getOutputStream();
				pw = new PrintWriter(os);
				pw.println(userInput);
				pw.flush();
				senderSocket.close();
				// close all resources when user types "quit"
				if (userInput.equalsIgnoreCase("quit")) {
					os.close();
					pw.close();
					System.out.println("Sender client terminated!");
					break;
				}
			} // end while
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} // end try...catch	

	} // end main method
	
} // end class Client





Is This A Good Question/Topic? 0
  • +

Replies To: Socket Issue: Message can only be forwarded for one time

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Socket Issue: Message can only be forwarded for one time

Posted 27 September 2015 - 07:34 AM

you need to rethink the structure of the code
for example, why do you close the senderSocket in line 27 of the SenderClient
in all the programs once you have the socket and the streams open leave them alone until you close down, don't close and open them again you get in a mess
e.g. should the SenderClient be
                        while (true) {
                                // create sender socket with host and port number
                                Socket senderSocket = new Socket("localhost", 8888);
                                // get message from user input
                                br = new BufferedReader(new InputStreamReader(System.in));
                                // get output stream, send message to server
                                os = senderSocket.getOutputStream();
                                pw = new PrintWriter(os);
                                while(true)
                                {
                                 System.out.println("Please input a message:");
                                 userInput = br.readLine();
                                  pw.println(userInput);
                                  pw.flush();
                                  // close all resources when user types "quit"
                                  if (userInput.equalsIgnoreCase("quit")) {
                                        senderSocket.close();
                                        os.close();
                                        pw.close();
                                        System.out.println("Sender client terminated!");
                                        break;
                                 }
                                }
 

This post has been edited by horace: 27 September 2015 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Socket Issue: Message can only be forwarded for one time

Posted 27 September 2015 - 08:33 AM

Quote

Message can only be forwarded for one time


The reason for that is your code is single threaded. For each client to behave independently and then for the system to reset itself once one of them sends "quit", you need multithreading
Was This Post Helpful? 1
  • +
  • -

#4 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Socket Issue: Message can only be forwarded for one time

Posted 27 September 2015 - 11:51 AM

I don't think we need threads at the moment as it is a fairly simple sequence, e.g.

SenderClient loop reading Keybaord - send packet to server
Server loop wait for packet from SenderClient - on recipt send to ReceiverClient
ReceiverClient loop wait for packet from server - display it

the main problem with the code seemed to be closing sockets after one transmission/recipt

threads would be the next extension so that the ReceiverClient could send a message back etc
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Socket Issue: Message can only be forwarded for one time

Posted 28 September 2015 - 03:58 AM

Quote

I don't think we need threads at the moment


I think i got the emphasis wrong there. What i'm trying to say is that to support the scenario of two independent clients that can either send a message to one another or send a QUIT message such that the system is reset, then multithreading is necessary. For one thing, the server can't be expected to know the intentions of its two clients.

At the moment, the single-thread server attempts to read from the sender (which is actually nothing more than the client that just happens to have connected second). If that client has nothing to say, the system will block and proceed no further.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1