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 10.0.1.15

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"));
	}
}
anthonydonx 0 Newbie Poster

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

anthonydonx 0 Newbie Poster

set ip to:

InetAddress host = InetAddress.getLocalHost();
String diffHost = "10.215.177.201";
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.