java program for Client-Server Program using TCP/IP

Program : Client-Server Program using TCP/IP
By : Kapil Lohia

Program 1:

 
import java.net.*;
import java.io.*;
 
class tcpip_server
{
	public static void main(String args[]) throws IOException
	{
		ServerSocket n1=null;
		try
		{
			n1=new ServerSocket(98);
		}
		catch(IOException e)
		{
			System.err.println("Port 98 could not be found");
			System.exit(1);
		}
		Socket c=null;
		try
		{
			c=n1.accept();
			System.out.println("Connection from "+c);
		}
		catch(IOException e)
		{
			System.out.println("Accept failed");
			System.exit(1);
		}
		PrintWriter out=new PrintWriter(c.getOutputStream(),true);
		BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream()));
		String n;
		BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Ready to type now");
		while((n=sin.readLine())!=null)
		{
			out.println(n);
		}
		out.close();
		c.close();
		n1.close();
	}
}

Program 2

 
import java.net.*;
import java.io.*;
 
class tcpip_client
{
	public static void main(String args[]) throws IOException
	{
		Socket s=null;
		BufferedReader b=null;
 
		try
		{
			s=new Socket(InetAddress.getLocalHost(),98);
			b=new BufferedReader(new InputStreamReader(s.getInputStream()));
		}
 
		catch(UnknownHostException u)
		{
			System.err.println("I don't know host");
			System.exit(0);
		}
		String inp;
		while((inp=b.readLine())!=null)
		{
			System.out.println(inp);
		}
		b.close();
		s.close();
	}
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

41 thoughts on “java program for Client-Server Program using TCP/IP

  1. pls find me solution for this program

    Write a client / server java program using TCP sockets, wherein the client sends the name of a program to be executed at the server. The server then sends the result of the executed program back to the client.

  2. Thanks for this beautiful simple program. This helped me in a very big problem.

    Keep it up.

  3. i have typed and compiled the above two programs in different cmd prompt.
    I got compiled and created .class files for both. But i am getting “no classdeffound error”. how to solve this prob and run this program.

  4. Thank you very much because it is very very useful for all the student. it is the best one and i like it i wish to stay tuned to end of my job. simple program is easy to understand and once again thank you very much sir…………….

  5. Design a TCP/UDP Client Server System which exchanges messages by performing Encryption /Decryption using Caesar Cipher. The Client sends the data by encrypting using Caesar cipher and the server decrypts the cipher text .Extend the dialogue between client and server for initial key exchange followed by Data.

  6. only server can send message to client.
    client can’t send message to server.
    what’s the use of this code ??????

  7. write a simple tcp/ip file server. the server replies the client by sending the list of available files and the responds with at one – line message , either “ok” or “error”.if the message is “ok”. it is followed by the contents of the files with the specified name. the “error” message indicates that the specified file doesnot exits on the server.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in