1.create Chat Application Using Either TCP or UDP Protocol.: Server Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1.Create chat application using either TCP or UDP protocol.

Server Code:
import java.net.*;
import java.io.*;
import java.util.*;

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

InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
while(true)
{
msg = dis.readUTF();
System.out.println(msg);

temp = sc.next();
dos.writeUTF(temp);
}
}
}

Client Code:
import java.net.*;
import java.io.*;
import java.util.*;

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

InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
while(true)
{
msg = sc.next();
dos.writeUTF(msg);

temp = dis.readUTF();
System.out.println(temp);
}
}
}

Output:
hi
hello
hell
what's up
2.Implement TCP Server for transferring files using Socket and
ServerSocket.
Server Code:
import java.net.*;
import java.io.*;

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

OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);

InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);

dos.writeUTF("1.Download");
dos.writeUTF("2.Upload");
dos.writeUTF("Enter your chocie");
int n=dis.readInt();
System.out.println("client cho "+n);

File f=new File("Upload");


//System.out.println(f.exists());

String name[]=f.list();
for(String t:name){
dos.writeUTF(t);
}
dos.writeUTF("exit");
String a=dis.readUTF();
boolean status=true;
for(String t:name){
if(t.equals(a))
{
status=false;
break;
}
}
if(status==true)
dos.writeUTF("fail");
else
{
FileInputStream fis=new FileInputStream("Upload/"+a);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String temp = br.readLine();
while(temp != null)
{
dos.writeUTF(temp);
//System.out.print((char)n);
temp = br.readLine();
}
dos.writeUTF("complete");
}
}
}

Client Code:
import java.util.*;
import java.net.*;
import java.io.*;

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

OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
String msg;
for(int i=0;i<3;i++)
{
msg=dis.readUTF();
System.out.println(msg);
}
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
dos.writeInt(n);
msg=dis.readUTF();
while(msg.equalsIgnoreCase("exit")==false)
{
System.out.println(msg);
msg=dis.readUTF();
}
System.out.println("Enter the file u want to dwld");
msg=sc.next();
dos.writeUTF(msg);
String temp;
temp = dis.readUTF();
FileOutputStream fos = new FileOutputStream("client_"+msg);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
while(temp.equalsIgnoreCase("complete")==false)
{
bw.write(temp,0,temp.length());
bw.newLine();
bw.flush();
temp = dis.readUTF();
}}}

Output:
CLIENT SIDE:
1.Download
2.Upload
Enter your chocie
1
1.txt
2.docx
3.txt
4.txt
Enter the file u want to dwld
1.txt

SERVER SIDE:
client cho 1
3.Implement 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 java.io.*;
import java.net.*;
import java.util.Arrays;

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

DataInputStream dis=new DataInputStream(s.getInputStream());


int n = dis.readInt();
int a[] = new int[n];
for (int i=0;i<n;i++)
{
a[i] = dis.readInt();
}
Arrays.sort(a);

DataOutputStream dos = new DataOutputStream(s.getOutputStream());


for (int i=0;i<n;i++)
{
dos.writeInt(a[i]);
}
ss.close();
s.close();
}
}

Client Code:
import java.io.*;
import java.net.*;
import java.util.Scanner;

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

System.out.println("Enter the length of array : ");


Scanner sc = new Scanner(System.in);
int msg = sc.nextInt();
dos.writeInt(msg);

System.out.println("Enter array elements : ");


int x = 0;
for (int i=0;i<msg;i++)
{
x = sc.nextInt();
dos.writeInt(x);
}
DataInputStream dis=new DataInputStream(s.getInputStream());
int a[] = new int[msg];
System.out.println("Sorted Array is : ");
for (int i=0;i<msg;i++)
{
a[i]=dis.readInt();
System.out.println(a[i]);
}
dos.flush();
dos.close();
s.close();
}
}

Output:
Enter the length of array :
4
Enter array elements :
169
78
25
200
Sorted Array is :
25
78
169
200
4.Implement 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 java.net.*;
import java.io.*;
class server
{
public static void main(String args[])throws Exception
{
String msg,msg1;
StringBuffer a,b;
ServerSocket ss=new ServerSocket(1000);
Socket s1=ss.accept();
Socket s2=ss.accept();

InputStream is1 = s1.getInputStream();


DataInputStream dis1 = new DataInputStream(is1);

InputStream is2 = s2.getInputStream();


DataInputStream dis2 = new DataInputStream(is2);

while(true)
{
msg=dis1.readUTF();
System.out.println(msg);
a=new StringBuffer(msg);
a.reverse();
System.out.println("Reverse String from client1:"+a);

msg1=dis2.readUTF();
System.out.println(msg1);
b=new StringBuffer(msg1);
b.reverse();
System.out.println("Reverse String from client2:"+b);
}
}
}
Client Code:
import java.net.*;
import java.util.*;
import java.io.*;
import java.lang.*;
class client
{
public static void main(String ar[])throws Exception
{
String msg;
String tmp;
Socket s = new Socket("localhost",1000);
Scanner sc=new Scanner(System.in);

OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);

while(true)
{
msg=sc.nextLine();
dos.writeUTF(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

You might also like