0% found this document useful (0 votes)
54 views7 pages

TCP Programs: Aim: A Simple of Java Socket Programming Where Client Sends A Text and Server Receives and Prints It

The document describes several Java socket programming examples using TCP for client-server communication. The examples include: 1) A simple client that sends a text message to a server, which receives and prints the message. 2) A chatting application where the client and server can exchange messages until one sends "stop". 3) A client that requests the date and time from a server, which responds with the server date and time. 4) A client that sends a string to a server, which checks if the string is a palindrome and responds accordingly.

Uploaded by

Tirth Gorasiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

TCP Programs: Aim: A Simple of Java Socket Programming Where Client Sends A Text and Server Receives and Prints It

The document describes several Java socket programming examples using TCP for client-server communication. The examples include: 1) A simple client that sends a text message to a server, which receives and prints the message. 2) A chatting application where the client and server can exchange messages until one sends "stop". 3) A client that requests the date and time from a server, which responds with the server date and time. 4) A client that sends a string to a server, which checks if the string is a palindrome and responds accordingly.

Uploaded by

Tirth Gorasiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

TCP Programs

Aim: A simple of Java socket programming where client sends a text and server receives and
prints it.

File: MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(7777);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}

File: MyClient.java
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",7777);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}

Aim: client will write first to the server then server will receive and print the text. Then
server will write to the client and client will receive and print the text. Until Server & client
send “stop” to eachother.
Or Aim: Chatting Application using TCP

File: MyServer.java
import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str="",str2="";
while(!str.equals("stop")){
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}}

File: MyClient.java
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}}

Aim: Write a TCP Client-Server program to get the Date & Time details from Server on
the Client request.

File:DateClient.java
import java.io.*;
import java.net.*;
class DateClient
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket(InetAddress.getLocalHost(),5217);
BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream() ));
System.out.println(in.readLine());
}
}

File:DateServer.java
import java.net.*;
import java.io.*;
import java.util.*;
class DateServer
{
public static void main(String args[]) throws Exception
{
ServerSocket s=new ServerSocket(5217);
while(true)
{
System.out.println("Waiting For Connection");
Socket soc=s.accept();
DataOutputStream out=new DataOutputStream(soc.getOutputStream());
out.writeBytes("Server Date: " + (new Date()).toString() + "\n");
out.close();
soc.close();
}
}
}

Aim: Write a client server program using TCP where client sends a string and server
checks whether that string is palindrome or not and responds with an appropriate message.

File:Client.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket("localhost",3777);
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println("nidhi");
InputStream is=s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str;
while((str=br.readLine())!=null)
{
System.out.println(str);
}
ps.close();
s.close();
}
}

File:Server.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
public static void main(String args[]) throws IOException
{
ServerSocket ss=new ServerSocket(3777);
Socket s=ss.accept();
InputStream is=s.getInputStream();
PrintStream ps=new PrintStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str=br.readLine();
String b="";
int n=str.length();
for(int i=n-1;i>=0;i--)
{
b=b+str.charAt(i);
}
if(str.equalsIgnoreCase(b))
{
ps.println(str);
ps.println("The string is palindrome.");
}
else
{
ps.println(str);
ps.println("The string is not palindrome.");
}
ps.close();
ss.close();
s.close();
}
}

You might also like