Sie sind auf Seite 1von 9

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

Coding:(Client)

import java.net.*;
import java.io.*;

class myclient1
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost", 6667);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
String str = "", str2 = "";
while (!str.equalsIgnoreCase("stop"))
{
//Reading next Input From Client
str = bin.readLine();
//writing to Server side

dout.writeUTF(str);
dout.flush();

//reading from server side

str2 = din.readUTF();
System.out.println("Server :" + str2);
}
dout.close();
s.close();
}
}
Coding:(Server)

import java.io.*;
import java.net.*;

class myserver
{
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(6667);
Socket s = ss.accept();
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader bin = new BufferedReader(newInputStreamReader(System.in));
String str = "", str2 = "";
while (!str.equalsIgnoreCase("stop"))
{
str = din.readUTF();
System.out.println("client says:" + str);
//Reading Next Input From Server
str2 = bin.readLine();
//Writing on client side
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
OUTPUT:

Client says:hi
Server says:hi

Client says:hello
Server says:hello
2. Implement TCP Server for transferring files using Socket and
ServerSocket.
Coding:(client)

import java.io.*;
import java.net.*;

class client
{
public static void main(String args[])
{
try
{

Socket s = new Socket("localhost", 6667);


System.out.println("Connected to server successfully");
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
File file = new File("rk.txt");
FileWriter fr = new FileWriter("rk.txt");
BufferedWriter br = new BufferedWriter(fr);
String str;
while (true)
{
str = din.readUTF();
br.write(str);
br.newLine();
br.flush();

}
catch (Exception e)
{
}
}
}
Coding:(server)

Import java.io.*;
import java.net.*;

class server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(6667);
Socket s = ss.accept();
System.out.println("Connected to client successfully");
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
File file = new File("myclient1.java");
FileReader fr = new FileReader("myclient1.java");
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null)
{
dout.writeUTF(str);
}

System.out.println("File is transfered Successfully");


br.close();
s.close();
ss.close();

}
}
OUTPUT:

Client side: Connected to server successfully


Server side: Connected to client successfully
File is transferred successfully

File(rk.txt):

import java.net.*;
import java.io.*;

class myclient1
{
public static void main(String args[]) throws Exception {
Socket s = new Socket("localhost", 6667);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader bin = new BufferedReader(new

InputStreamReader(System.in));

String str = "", str2 = "";


while (!str.equalsIgnoreCase("stop"))
{
//Reading next Input From Client
str = bin.readLine();
//writing to Server side
dout.writeUTF(str);
dout.flush();
//reading from server side
str2 = din.readUTF();
System.out.println("Server Says:" + str2);
}
dout.close();
s.close();
}
}

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.

Coding:(client)
import java.io.*;
import java.net.*;
import java.util.*;

class client
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost", 6667);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
int n;
system.out.println(How many data:);
Scanner obj = new Scanner(System.in);
n = obj.nextInt();
dout.writeInt(n);
int a[] = new int[n];
System.out.println("Enter data");

for (int i = 0; i < n; i++)


{
a[i] = obj.nextInt();
}
for (int i = 0; i < n; i++)
{
dout.writeInt(a[i]);
}
System.out.println("Receiving Data:");
for (int i = 0; i < n; i++)
{
a[i] = din.readInt();
}
for (int i = 0; i < n; i++)
{
System.out.println(a[i]);
}
}
}
Coding:(server)

import java.io.*;
import java.net.*;
import java.util.*;

class server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(6667);
Socket s = ss.accept();
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
int n;
n = din.readInt();
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = din.readInt();
}

for (int i = 0; i < n; i++)


{
System.out.println(a[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
dout.writeInt(a[i]);
}
s.close();
}
}
OUTPUT:

Client side:

How many data:


3

Enter the data

46
27
5

Receiving data:

5
27
46

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.
Coding:(client)

import java.io.*;
import java.net.*;
import java.util.*;

class client
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost", 6667);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
String str_out, str_in;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the String:");
str_out = obj.nextLine();
dout.writeUTF(str_out);
str_in = din.readUTF();
System.out.println(str_in);
s.close();
}
}
Coding:(server)

import java.io.*;
import java.net.*;

class server extends Thread


{
Socket s;
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(6667);
while (true)
{
Socket s = ss.accept();
server tt = new server(s);
Thread t = new Thread(tt);
t.start();
}
}
public server(Socket s)
{
this.s = s;
}
public void run()
{
try
{
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
String str_in, str_out;
str_in = din.readUTF();
StringBuffer str = new StringBuffer(str_in);
str.reverse();
str_out = str.toString();
dout.writeUTF(str_out);
s.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
OUTPUT:

(Client 1)

Enter the string:

Hi I am RUTUL..
..LUTUR am I iH

(Client 2)

Enter the string:

J2EE applications are made up of components


Stnenopmoc fo pu edam era snoitacilppa EE2J

(Client 3)

Enter the string:

A J2EE client can be a web client or an application client..


..tneilc noitacilppa na ro tneilc bew a eb nac tneilc EE2J A

Das könnte Ihnen auch gefallen