Java Chat Application with TCP/UDP

0% found this document useful (0 votes)
796 views10 pages
The document contains code for implementing a basic TCP chat application using sockets in Java. It includes server and client code that allows for bidirectional communication between two par…

Uploaded by

Kevin Vaghasiya
  • Chat Application using TCP/UDP
  • TCP Server for File Transfer
  • Sorting Algorithm Implementation using TCP/UDP
  • Concurrent TCP Server for String Reversal

[Link] chat application using either TCP or UDP protocol.

Server Code:
import [Link].*;
import [Link].*;
import [Link].*;

class server
{
public static void main(String ar[]) throws Exception
{
String msg;
String temp;
Scanner sc = new Scanner([Link]);
ServerSocket ss=new ServerSocket(1000);
Socket s = [Link]();

InputStream is = [Link]();
OutputStream os = [Link]();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
while(true)
{
msg = [Link]();
[Link](msg);

temp = [Link]();
[Link](temp);
}
}
}

Client Code:
import [Link].*;
import [Link].*;
import [Link].*;

class client
{
public static void main(String ar[]) throws Exception
{
String msg;
String temp;
Socket s = new Socket("localhost",1000);
Scanner sc = new Scanner([Link]);

InputStream is = [Link]();
OutputStream os = [Link]();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
while(true)
{
msg = [Link]();
[Link](msg);

temp = [Link]();
[Link](temp);
}
}
}

Output:
hi
hello
hell
what's up
[Link] TCP Server for transferring files using Socket and
ServerSocket.
Server Code:
import [Link].*;
import [Link].*;

class server
{
public static void main(String ar[])throws Exception
{
ServerSocket ss=new ServerSocket(1000);
Socket s=[Link]();

OutputStream os=[Link]();
DataOutputStream dos=new DataOutputStream(os);

InputStream is=[Link]();
DataInputStream dis=new DataInputStream(is);

[Link]("[Link]");
[Link]("[Link]");
[Link]("Enter your chocie");
int n=[Link]();
[Link]("client cho "+n);

File f=new File("Upload");


//[Link]([Link]());

String name[]=[Link]();
for(String t:name){
[Link](t);
}
[Link]("exit");
String a=[Link]();
boolean status=true;
for(String t:name){
if([Link](a))
{
status=false;
break;
}
}
if(status==true)
[Link]("fail");
else
{
FileInputStream fis=new FileInputStream("Upload/"+a);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String temp = [Link]();
while(temp != null)
{
[Link](temp);
//[Link]((char)n);
temp = [Link]();
}
[Link]("complete");
}
}
}

Client Code:
import [Link].*;
import [Link].*;
import [Link].*;

class client
{
public static void main(String ar[])throws Exception
{
Socket s=new Socket("localhost",1000);

OutputStream os=[Link]();
DataOutputStream dos=new DataOutputStream(os);
InputStream is=[Link]();
DataInputStream dis=new DataInputStream(is);
String msg;
for(int i=0;i<3;i++)
{
msg=[Link]();
[Link](msg);
}
Scanner sc=new Scanner([Link]);
int n=[Link]();
[Link](n);
msg=[Link]();
while([Link]("exit")==false)
{
[Link](msg);
msg=[Link]();
}
[Link]("Enter the file u want to dwld");
msg=[Link]();
[Link](msg);
String temp;
temp = [Link]();
FileOutputStream fos = new FileOutputStream("client_"+msg);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
while([Link]("complete")==false)
{
[Link](temp,0,[Link]());
[Link]();
[Link]();
temp = [Link]();
}}}

Output:
CLIENT SIDE:
[Link]
[Link]
Enter your chocie
1
[Link]
[Link]
[Link]
[Link]
Enter the file u want to dwld
[Link]

SERVER SIDE:
client cho 1
[Link] any one sorting algorithm using TCP/UDP on Server
application and give Input on Client side and client should sorted
output from server and display sorted on input side.
Server Code:
import [Link].*;
import [Link].*;
import [Link];

class server2
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(1000);
Socket s=[Link]();

DataInputStream dis=new DataInputStream([Link]());


int n = [Link]();
int a[] = new int[n];
for (int i=0;i<n;i++)
{
a[i] = [Link]();
}
[Link](a);

DataOutputStream dos = new DataOutputStream([Link]());


for (int i=0;i<n;i++)
{
[Link](a[i]);
}
[Link]();
[Link]();
}
}

Client Code:
import [Link].*;
import [Link].*;
import [Link];

class client2
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost", 1000);
DataOutputStream dos=new DataOutputStream([Link]());

[Link]("Enter the length of array : ");


Scanner sc = new Scanner([Link]);
int msg = [Link]();
[Link](msg);

[Link]("Enter array elements : ");


int x = 0;
for (int i=0;i<msg;i++)
{
x = [Link]();
[Link](x);
}
DataInputStream dis=new DataInputStream([Link]());
int a[] = new int[msg];
[Link]("Sorted Array is : ");
for (int i=0;i<msg;i++)
{
a[i]=[Link]();
[Link](a[i]);
}
[Link]();
[Link]();
[Link]();
}
}

Output:
Enter the length of array :
4
Enter array elements :
169
78
25
200
Sorted Array is :
25
78
169
200
[Link] Concurrent TCP Server programming in which more
than one client can connect and communicate with Server for
sending the string and server returns the reverse of string to each of
client.

Server Code:
import [Link].*;
import [Link].*;
class server
{
public static void main(String args[])throws Exception
{
String msg,msg1;
StringBuffer a,b;
ServerSocket ss=new ServerSocket(1000);
Socket s1=[Link]();
Socket s2=[Link]();

InputStream is1 = [Link]();


DataInputStream dis1 = new DataInputStream(is1);

InputStream is2 = [Link]();


DataInputStream dis2 = new DataInputStream(is2);

while(true)
{
msg=[Link]();
[Link](msg);
a=new StringBuffer(msg);
[Link]();
[Link]("Reverse String from client1:"+a);

msg1=[Link]();
[Link](msg1);
b=new StringBuffer(msg1);
[Link]();
[Link]("Reverse String from client2:"+b);
}
}
}
Client Code:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class client
{
public static void main(String ar[])throws Exception
{
String msg;
String tmp;
Socket s = new Socket("localhost",1000);
Scanner sc=new Scanner([Link]);

OutputStream os=[Link]();
DataOutputStream dos=new DataOutputStream(os);

while(true)
{
msg=[Link]();
[Link](msg);
}
}
}

Output:
CLIENT 1
hi
hello
how are u??
CLIENT 2
he
how are u??
SERVER
hi
Reverse String from client1 :ih
he
Reverse String from client2 :eh
hello
Reverse String from client1 :olleh
how are u??
Reverse String from client2 :??u era woh
how are u??
Reverse String from client1 :??u era woh

1.Create chat application using either TCP or UDP protocol. 
 
Server Code: 
import java.net.*; 
import java.io.*; 
import
{ 
 
String msg; 
 
String temp; 
 
Socket s = new Socket("localhost",1000); 
 
Scanner sc = new Scanner(System.in);
2.Implement TCP Server for transferring files using Socket and   
ServerSocket. 
Server Code: 
import java.net.*; 
import jav
else 
 
 
{ 
 
 
 
FileInputStream fis=new FileInputStream("Upload/"+a); 
 
 
 
InputStreamReader isr = new InputStreamRe
msg=dis.readUTF(); 
 
 
} 
 
 
System.out.println("Enter the file u want to dwld"); 
 
 
msg=sc.next(); 
 
 
dos.writeU
3.Implement any one sorting algorithm using TCP/UDP on Server 
application and give Input on Client side and client should so
public static void main(String args[]) throws Exception  
  { 
   Socket s = new Socket("localhost", 1000); 
DataOutputStre
4.Implement Concurrent TCP Server programming in which more 
than one client can connect and communicate with Server for 
sen
Client Code: 
import java.net.*; 
import java.util.*; 
import java.io.*; 
import java.lang.*; 
class client 
{ 
public static

You might also like