Results 1 to 6 of 6
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Sending more than one message to the server.

    I've made a function in the client code to send messages to the server, and i've written code so that the server is able to read them. Problem is, I can only send one message - why is that?

    Here's the code:
    Client Side: Sending A Message
    Java Code:
    public void actionPerformed(ActionEvent event)
    {
    	try{
    		writeToServer.println(outgoingMessageField.getText());
    		writeToServer.flush();
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }
    Server Side: Receiving A Message
    Java Code:
    public void startServer()
    {
    	try{
    		ServerSocket server = new ServerSocket(8558);
    		if(server.getLocalSocketAddress()!=null)
    		{
    			System.out.printf("Server running.\n\tAddress:%s\n", server.getLocalSocketAddress(),server.getLocalPort());
    		}else
    		{
    			System.out.println("Failure");
    		}
    		while(true)
    		{
    			Socket client = server.accept();
    			reader = new InputStreamReader(client.getInputStream());
    			buffyTheFileReader = new BufferedReader(reader);
    			incomingMessage = buffyTheFileReader.readLine();
    			if(incomingMessage.isEmpty()==false)
    			{
    				this.serverActivityNumber++;
    				System.out.printf("%d. Message Received from \t%s:\t\"%s\"\n", this.serverActivityNumber,client.getLocalAddress(),this.incomingMessage);
    			}
    				
    		}//...
    Thanks for the help!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Post the complete error you're getting, if any.

    I notice that your call to ServerSocket.accept() is inside your main loop. Meaning that only one message can be sent on each client connection, and then the server goes back to waiting for a connection. Is that what you intended?
    Get in the habit of using standard Java naming conventions!

  3. #3
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    That's precisely what I didn't intend. I want to be able to send multiple messages - how would I do that.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Put the following three lines above the while loop:

    Java Code:
    Socket client = server.accept();
    reader = new InputStreamReader(client.getInputStream());
    buffyTheFileReader = new BufferedReader(reader);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    @Josah - I didn't notice that! You're absolutely right. Big Waste of Memory!

    OK, this is the updated version of the Server Side Code:

    Java Code:
    public void startServer()
    	{
    		try{
    			ServerSocket server = new ServerSocket(8558);
    			if(server.getLocalSocketAddress()!=null)
    			{
    				System.out.printf("Server running.\n\tAddress:%s\n", server.getLocalSocketAddress(),server.getLocalPort());
    			}else
    			{
    				System.out.println("Failure");
    			}
    			
    				Socket client = server.accept();
    				this.client=client;
    				reader = new InputStreamReader(this.client.getInputStream());
    				buffy = new BufferedReader(reader);
    			
    			
    		}catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    	
    	public void receiveMessages()
    	{
    		System.out.println("Ready to receive messages.");
    		while(client.isConnected()==true)
    		{
    				try{
    					if((this.incomingMessage=this.buffy.readLine())!=null)
    					{
    						this.serverActivityNumber++;
    						System.out.printf("%d. Message Received from %s:\t %s.\n ", this.serverActivityNumber,this.client.getLocalAddress(),this.incomingMessage);
    					}
    				}catch(IOException e)
    				{
    					e.printStackTrace();
    				}
    			
    		}
    	}
    I have a question. The while loop has the condition that
    Java Code:
    while(client.isConnected()==true){//instructions}
    . If I understand right, that means as long as the client is connected to the server, the while-loop condition is true and the instructions are executed. If I close the client side application, the while loop condition should be false and the instruction shouldn't be executed.

    However, for some reason that last bit doesn't hold. When I close the client side application - the while loop still executes and it prints the stack trace. What's wrong with this condition?

    Thanks for your help!

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    The documentation makes it sound like "hasBeenConnected" would be a better name for the "isConnected" method. It returns "true if the socket successfuly connected to a server."

    I always just assume the other end of a socket is going to close it and some read or write on my end is going to throw an exception. Design your loop accordingly.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Urgent-how to get xml message before sending in webservice
    By mydesktop in forum Web Frameworks
    Replies: 6
    Last Post: 02-17-2011, 02:59 PM
  2. sending message to phones
    By sivasankar in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-16-2011, 08:56 AM
  3. Sending a message to a flash chat
    By Jman85 in forum New To Java
    Replies: 3
    Last Post: 11-08-2010, 06:07 PM
  4. [SOLVED] Sending a message to clients using RMI
    By ChrisMayhew in forum New To Java
    Replies: 11
    Last Post: 01-12-2009, 07:29 PM
  5. sending the message from trigger to mailid
    By geeta_ravikanti in forum JDBC
    Replies: 0
    Last Post: 04-04-2008, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •