I am just learning how to use sockets, and I have managed to get my server to send a message to my socket, but when I try to send a message from my socket to my server I get an error. Here are my codes

Server:

package BasicNIO;

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

public class ServerSockets {

	ServerSocket servSock;
	Socket sock;
	
	public ServerSockets (){
		
		// creates a new server socket at port 5555
		try {
			servSock = new ServerSocket (5555);
			 System.out.println("Server Started: " + servSock);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// creates a socket that uses the server socket
		
		 try {
			sock = servSock.accept();
			System.out.println("Client Connected: " + sock);
			
			PrintStream ps = new PrintStream (sock.getOutputStream ());
			ps.println ("Server: Hello"); // sends message
			ps.close(); // closes print stream
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 System.out.println(" Connection from :  " + sock.getInetAddress());
		 
		  try {
			DataInputStream is = new DataInputStream(sock.getInputStream());
			System.out.println(is.readLine());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			sock.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // closes socket connection
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ServerSockets ss = new ServerSockets ();
		
		
	}

}

Client:

package BasicNIO;

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

public class Sockets {

	Socket sock;
	
	public Sockets (){
		InetAddress ip;
		try {
			ip = InetAddress.getByName
			 ("192.168.0.100");
			sock = new Socket (ip,5555);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			PrintStream ps = new PrintStream(sock.getOutputStream());
			ps.println ("Client: Hi Server");
			DataInputStream is = new DataInputStream(sock.getInputStream());
			String message = is.readLine();
			System.out.println (message);
			ps.close();
			sock.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main (String [] args){
		Sockets s = new Sockets ();
		
		
		
	}
	
}

Thanks for any help.

I try to send a message from my socket to my server I get an error

Can you post the full text of the error message?

output for ServerSockets

Server Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=5555]
java.net.SocketException: Socket is closed
	at java.net.Socket.getInputStream(Unknown Source)
	at BasicNIO.ServerSockets.<init>(ServerSockets.java:39)
	at BasicNIO.ServerSockets.main(ServerSockets.java:56)
Client Connected: Socket[addr=/192.168.0.100,port=57223,localport=5555]
 Connection from :  /192.168.0.100

output for Sockets

Server: Hello

It shouldn't be closed. I close the socket after I try to get the message sent by the client.

line 39 & 40 I get try to get the message

line 46 I close the socket

I prefer to test client/server code in the same JVM. That way the debug output shows the order of events in both sides.

Who closes the socket connection first?

I don't know which one closes the socket first. I think when the the server tries to read the message the client either didn't send the message (and therefore I cannot read it), or the Client didn't connect at the time when I try to read it.

The second option is valid, because it displays the error, and then it prints out that the client connected.

Also how do I test two programs in the same vm?

Edited 5 Years Ago by sirlink99: n/a

Make then internal classes and add a main:

public class ClientServerSockets {

	public static void main(String[] args) {
            Thread t = new Thread(new Runnable() {
                 public void run() {
		    ServerSockets ss = new ServerSockets ();
                 }
            });
            t.start();
 
            Sockets s = new Sockets ();
        } //end main()

Edited 5 Years Ago by NormR1: n/a

This is the output that I get

Server Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=5555]
java.net.SocketException: Socket is closed
	at java.net.Socket.getInputStream(Unknown Source)
	at BasicNIO.ServerSockets.<init>(ServerSockets.java:38)
	at SocketExamples.testTogether$1.run(testTogether.java:17)
	at java.lang.Thread.run(Unknown Source)
Client Connected: Socket[addr=/192.168.0.100,port=58134,localport=5555]
 Connection from :  /192.168.0.100
Server: Hello

from this code

package BasicNIO;

import java.awt.event.*;


public class testTogether {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 Thread t = new Thread(new Runnable() {
             public void run() {
            	 ServerSockets ss = new ServerSockets ();
             }
        });
		t.start();
		Sockets s = new Sockets();
		
	}

}

which runs my previous codes

Edited 5 Years Ago by sirlink99: n/a

Now its time to add lots of print statement to show when everything is happening.
Enter a method.
Open a connection
close anything
Be sure to add a prefix to the messages so you know who printed it. For example:

System.out.println("S - Client Connected: " + sock);
System.out.println ("C msg=" + message);
System.out.println("S read=" + is.readLine());

Edited 5 Years Ago by NormR1: n/a

This is the output I get when I add in a bunch of printlns

S - Server Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=5555]
S - Waiting for a Connection
C - Setting up adress
C - Creating socket
C - Socket Created
C - Creating PrintStream
C - Created! Sending Message
C - Message Sent
C - Creating DataInputStream
C - Created. Attempting to read message
java.net.SocketException: Socket is closed
	at java.net.Socket.getInputStream(Unknown Source)
	at BasicNIO.ServerSockets.<init>(ServerSockets.java:44)
	at BasicNIO.testTogether$1.run(testTogether.java:15)
	at java.lang.Thread.run(Unknown Source)
S - Connection Accepted
Client Connected: Socket[addr=/192.168.0.100,port=58320,localport=5555]
S - Sending message
S - Message Sent
S - PrintStream Closed
 Connection from :  /192.168.0.100
S - Entering reading try-catch statement
S - Creating DataInputStream
C - Message read, and assigned to a variable 'message'
C - Printing message
Server: Hello
C - Message Printed. Closing DataInputStream
C - Closed. Closing PrintStream
C - Closed. Closing Socket
C - Closed

I do have printlns before and after each close().

Edited 5 Years Ago by sirlink99: n/a

I don't know why your printed "closing" message doesn't print out before the "Socket is closed" message.

When I restore the code I get:

Client starting
Server Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=5555]
S - Client Connected: Socket[addr=/192.168.1.10,port=4622,localport=5555]
C msg=Server: Hello
C closing PS
C closing socket
C socket closed
S closing PS
S - Connection from :  /192.168.1.10
java.net.SocketException: Socket is closed
	at java.net.Socket.getInputStream(Socket.java:788)
	at ClientServerSockets$ServerSockets.<init>(ClientServerSockets.java:61)
	at ClientServerSockets$1.run(ClientServerSockets.java:17)
	at java.lang.Thread.run(Thread.java:662)

0 error(s)

Edited 5 Years Ago by NormR1: n/a

here is my code that has all of the printlns in it

Server

package BasicNIO;

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

public class ServerSockets {

	ServerSocket servSock;
	Socket sock;
	
	public ServerSockets (){
		
		// creates a new server socket at port 5555
		try {
			servSock = new ServerSocket (5555);
			 System.out.println("S - Server Started: " + servSock);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// creates a socket that uses the server socket
		
		 try {
			System.out.println ("S - Waiting for a Connection");
			sock = servSock.accept();
			System.out.println ("S - Connection Accepted");
			System.out.println("Client Connected: " + sock);
			PrintStream ps = new PrintStream (sock.getOutputStream ());
			System.out.println ("S - Sending message");
			ps.println ("Server: Hello"); // sends message
			System.out.println ("S - Message Sent");
			ps.close(); // closes print stream
			System.out.println ("S - PrintStream Closed");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 System.out.println(" Connection from :  " + sock.getInetAddress());
		 System.out.println ("S - Entering reading try-catch statement");
		  try {
			System.out.println ("S - Creating DataInputStream");
			DataInputStream is = new DataInputStream(sock.getInputStream());
			System.out.println ("S - Created! Trying to recieve Message");
			System.out.println(is.readLine());
			System.out.println ("S - Closing socket");
			sock.close();
			System.out.println ("S - Socket Closed");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ServerSockets ss = new ServerSockets ();
		
		
	}

}

Client

package BasicNIO;

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

public class Sockets {

	Socket sock;
	
	public Sockets (){
		InetAddress ip;
		try {
			System.out.println ("C - Setting up address");
			ip = InetAddress.getByName
			 ("192.168.0.100");
			System.out.println ("C - Creating socket");
			sock = new Socket (ip,5555);
			System.out.println ("C - Socket Created");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			System.out.println ("C - Creating PrintStream");
			PrintStream ps = new PrintStream(sock.getOutputStream());
			System.out.println ("C - Created! Sending Message");
			ps.println ("Client: Hi Server");
			System.out.println ("C - Message Sent");
			System.out.println ("C - Creating DataInputStream");
			DataInputStream is = new DataInputStream(sock.getInputStream());
			System.out.println ("C - Created. Attempting to read message");
			String message = is.readLine();
			System.out.println ("C - Message read, and assigned to a variable 'message'");
			System.out.println ("C - Printing message");
			System.out.println (message);
			System.out.println ("C - Message Printed. Closing DataInputStream");
			is.close();
			System.out.println ("C - Closed. Closing PrintStream");
			ps.close();
			System.out.println ("C - Closed. Closing Socket");
			sock.close();
			System.out.println ("C - Closed");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main (String [] args){
		Sockets s = new Sockets ();
		
		
		
	}
	
}

Sorry, no idea what is happening on your system.
When I removed all the close() calls it worked for me.

Strange it works for me. Here's my working version:

/*http://www.daniweb.com/software-development/java/threads/383023/1649558#post1649558

*/


//package BasicNIO;

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

public class ClientServerSockets {

	public static void main(String[] args) {
      Thread t = new Thread(new Runnable() {
        public void run() {
		    ServerSockets ss = new ServerSockets ();
        }
      });
      t.start();
 
		Sockets s = new Sockets ();
   }



static class ServerSockets {

	ServerSocket servSock;
	Socket sock;
	
	public ServerSockets (){
		
		// creates a new server socket at port 5555
		try {
			servSock = new ServerSocket (5555);
			 System.out.println("Server Started: " + servSock);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// creates a socket that uses the server socket
		
		 try {
			sock = servSock.accept();
			System.out.println("S - Client Connected: " + sock);
			
			PrintStream ps = new PrintStream (sock.getOutputStream ());
			ps.println ("Server: Hello"); // sends message
//         System.out.println("S closing PS");
//			ps.close(); // closes print stream
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 System.out.println("S - Connection from :  " + sock.getInetAddress());
		 
		  try {
			DataInputStream is = new DataInputStream(sock.getInputStream());  // Socket closed here
			System.out.println("S read=" + is.readLine());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			sock.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // closes socket connection
	}
	
	

}
/*
package BasicNIO;

import java.io.*;
import java.net.*;
import java.nio.*;
*/
static class Sockets {

	Socket sock;
	
	public Sockets (){
      System.out.println("Client starting");
		InetAddress ip;
		try {
			ip = InetAddress.getByName("192.168.1.10"); //192.168.0.100");
			sock = new Socket (ip, 5555);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			PrintStream ps = new PrintStream(sock.getOutputStream());
			ps.println ("Client: Hi Server");
			DataInputStream is = new DataInputStream(sock.getInputStream());
			String message = is.readLine();
			System.out.println ("C msg=" + message);
//         System.out.println("C closing PS");
//			ps.close();
//         System.out.println("C closing socket");
//			sock.close();
//         System.out.println("C socket closed");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

} // end class
This article has been dead for over six months. Start a new discussion instead.