Command line Client-Server via Socket Programming

Rajnesh 0 Tallied Votes 3K Views Share

start Server first
start Client by passing the ipaddress of the server
eg; java Client

if an error pops up change the port in both Client and Server programs

/*************************
Server.java
*************************/
import java.io.*;
import java.net.*;

public class  Server
{	
	public static void main(String[] args) throws IOException
	{
		int PORT = 8134;
		InputStream inStream;
		DataInputStream inDataStream;
		OutputStream outStream;
		DataOutputStream outDataStream;
		String message="";
		String received="";

		System.out.println("Chat Server Started");

		ServerSocket sock = new ServerSocket(PORT);
		Socket conn = sock.accept();	
		do{
		inStream = conn.getInputStream ();
		inDataStream = new DataInputStream ( inStream );
		message = inDataStream.readUTF();		
		System.out.println("Client sent: "+message);

		DataInputStream dis = new DataInputStream(System.in);
		message = dis.readLine();
		outStream = conn.getOutputStream();
		outDataStream = new DataOutputStream (outStream);	
		System.out.println("Enter your message here: ");
		outDataStream.writeUTF(message);	
		}while(!message.equals("bye"));
		conn.close();
	}
}


/******************************
Client.java
*****************************/
import java.net.*;
import java.io.*;

public class Client 
{		
	public static void main(String args[]) throws IOException
	{
		int PORT = 8134;
		InputStream inStream;
		DataInputStream inDataStream;
		OutputStream outStream;
		DataOutputStream outDataStream;
		String message = "";
			
		InetAddress host = InetAddress.getLocalHost();
		String diffHost = args[0];
		Socket sock = new Socket(diffHost,PORT);
		System.out.println("Chat Client Started");
		do{
			System.out.println("Enter your message here: ");
			DataInputStream dis = new DataInputStream(System.in);
			message = dis.readLine();
			outStream = sock.getOutputStream();
			outDataStream = new DataOutputStream (outStream);		
			outDataStream.writeUTF(message);	

			inStream = sock.getInputStream ();
			inDataStream = new DataInputStream ( inStream );
			message = inDataStream.readUTF();
			System.out.println("Server Sent: "+message);
		}while(!message.equals("bye"));
	}
}

Dani AI

Generated

A couple of gotchas here explain both the IP question and the "Connection reset" you are seeing.

  • : the client must connect to the server machine’s reachable IP, not the client’s. If both run on the same box, use 127.0.0.1. If they are on the same LAN, use the server’s LAN address (e.g., 10.x/192.168.x). Make sure the server is listening on that interface and the port is open through any firewall.

  • That SocketException almost always means the peer closed the TCP connection while you were reading. Common causes: protocol mismatch (one side calls readUTF while the other sends plain text), the server exits or closes early, or ports don’t actually match. In your case, calling readUTF on the client while the server sends something else (or nothing yet) will abort when the server closes.

Pick one framing and use it symmetrically. Either use Data streams on both sides with writeUTF/readUTF, or switch both sides to line-based text. For a simple, line-based protocol that works with Telnet/netcat and avoids length-prefix issues:

BufferedReader in = new BufferedReader(
    new InputStreamReader(sock.getInputStream(), java.nio.charset.StandardCharsets.UTF_8));
java.io.PrintWriter out = new java.io.PrintWriter(
    new java.io.OutputStreamWriter(sock.getOutputStream(), java.nio.charset.StandardCharsets.UTF_8), true);

// write, then read
out.println(message);                // auto-flush = true
String reply = in.readLine();

Practical checks:

  • Verify the server is actually listening: netstat -an | find ":PORT"
  • From the client host: nc -vz SERVER_IP PORT (or telnet SERVER_IP PORT)
  • Create streams once per connection and reuse them; do not re-wrap them inside a loop as in ’s snippet.
  • Ensure one side writes while the other reads to avoid deadlocks.
  • For easier debugging, set a read timeout: socket.setSoTimeout(5000).
anthonydonx 0 Newbie Poster

what ip ??????its sever running ip?

anthonydonx 0 Newbie Poster

set ip to:

InetAddress host = InetAddress.getLocalHost();
String diffHost = "";
Socket sock = new Socket(diffHost,PORT);
System.out.println("Chat Client Started");


& run client fallowing exception throw>>>>>>

run:
Chat Client Started

Enter your message here:
sdsds
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:185)
at java.net.SocketInputStream.read(SocketInputStream.java:199)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at chat.Client.main(Client.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

jt86442 -1 Light Poster

Use this instead of set up ip...

ServerSocket sock = new ServerSocket(PORT);		Socket conn = sock.accept();			do{		inStream = conn.getInputStream ();		inDataStream = new DataInputStream ( inStream );		message = inDataStream.readUTF();				System.out.println("Client sent: "+message); 		DataInputStream dis = new DataInputStream(System.in);		message = dis.readLine();		outStream = conn.getOutputStream();		outDataStream = new DataOutputStream (outStream);			System.out.println("Enter your message here: ");		outDataStream.writeUTF(message);			}while(!message.equals("bye"));		conn.close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.