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();
}
} |
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();
}
} |
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
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.
java program for Client-Server Program using TCP/IP
nice code
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.
Thanks for this beautiful simple program. This helped me in a very big problem.
Keep it up.
thank u gud to understant
Thank u very much………
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.
GUD YAR
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…………….
how to executed
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.
please send me the code urgent
only server can send message to client.
client can’t send message to server.
what’s the use of this code ??????
Is it really TCP. I think its just udp socket program…..
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.
Thanks friend. Very useful code.
pls i want a program to ths question
nice program….
how to send a file using this code…?
I need complete code to send message to the server and read it from my app.
hey check out this simple program http://www.javaengineeringprograms.com/client-server-program-in-java/