Java TCP Udp
Java TCP Udp
Java TCP Udp
Server.java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception
{
DatagramSocket socket=new DatagramSocket(9861);
byte receiveByte[]=new byte[1024];
byte sendByte[]=new byte[1024];
while(true)
{
//data receiving
DatagramPacketreceivePacket=new
DatagramPacket(receiveByte,receiveByte.length);
socket.receive(receivePacket);
String receiveStr=new String(receivePacket.getData());
receiveStr=receiveStr.trim();
System.out.println("Client:"+receiveStr);
//data sending
DataInputStream din=new DataInputStream(System.in);
System.out.print("Server:");
String sendStr=din.readLine();
sendByte=sendStr.getBytes();
InetAddressip=receivePacket.getAddress();
int port=receivePacket.getPort();
DatagramPacketsendPacket=new
DatagramPacket(sendByte,sendByte.length,ip,port);
socket.send(sendPacket);
}
}
}
Client.java
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws Exception
{
DatagramSocket socket=new DatagramSocket();
InetAddressip=InetAddress.getLocalHost();
byte sendByte[]=new byte[1024];
byte receiveByte[]=new byte[1024];
while(true)
{
//Data Sending
DataInputStream din=new DataInputStream(System.in);
System.out.print("Client:");
String sendStr=din.readLine();
sendByte=sendStr.getBytes();
DatagramPacketsendPacket=new
DatagramPacket(sendByte,sendByte.length,ip,9861);
socket.send(sendPacket);
//Data Receiving
DatagramPacketreceivePacket=new
DatagramPacket(receiveByte,receiveByte.length);
socket.receive(receivePacket);
String receiveStr=new String(receivePacket.getData());
receiveStr=receiveStr.trim();
System.out.println("Server:"+receiveStr);
}
}
Output:
Client:hi
Server:hello
Implement TCP server for transferring files using socket and
ServerSocket.
Server.java
import java.io.*;
import java.net.*;
class FileServer
{
public static void main(String ar[]) throws Exception
{
ServerSocket s1=new ServerSocket(12345);
System.out.println("SERVER STARTED:\nWaiting for client");
Socket s=s1.accept();
System.out.println("client connected");
PrintWriter p=new PrintWriter(s.getOutputStream(),true);
BufferedReader in=new BufferedReader(new
InputStreamReader(s.getInputStream()));
String st=in.readLine();
FileReader fr=new FileReader(st);
BufferedReader br=new BufferedReader(fr);
String line=new String();
System.out.println("Reading file "+st);
System.out.print("sending to client");
for(int i=1;i<10;i++)
{
System.out.print(".");
Thread.sleep(100);
}
while((line=br.readLine())!=null)
{
p.println(line);
}
p.flush();
s.close();
}
}
Client.java
import java.io.*;
import java.net.*;
class FileClient
{
public static void main(String ar[]) throws Exception
{
Socket s=new Socket("localhost",12345);
DataOutputStream dos=new
DataOutputStream(s.getOutputStream());
BufferedReader in=new BufferedReader(new
InputStreamReader(s.getInputStream()));
BufferedReader ink=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter file name::");
String s1=ink.readLine();
dos.writeBytes(s1+"\n");
String sr;
while((sr=in.readLine())!=null)
{
System.out.println(sr);
}
s.close();
}
}
Output:
Implement any one sorting algorithm using TCP or UDP on
server application and give input on client side and client
should sorted output from server and display sorted on input
side.
Server.java
import java.io.*;
import java.net.*;
class SortServer
{
public static void main(String ar[]) throws Exception
{
ServerSocket s1=new ServerSocket(12345);
System.out.println("Server Started");
Socket s=s1.accept();
PrintWriter p=new PrintWriter(s.getOutputStream());
BufferedReader in=new BufferedReader(new
InputStreamReader(s.getInputStream()));
String num=in.readLine();
int n=Integer.parseInt(num);
System.out.println("Client want to sort "+n+" numbers");
String sarr[]=new String[n];
int arr[]=new int[n];
int swap,c,d;
System.out.println("received numbers::\n");
for(int i=0;i<n;i++)
{
sarr[i]=in.readLine();
arr[i]=Integer.parseInt(sarr[i]);
System.out.println("no. "+i+"="+arr[i]);
}
}
Client.java
import java.io.*;
import java.net.*;
class SortClient
{
public static void main(String ar[]) throws Exception
{
Socket s=new Socket("localhost",12345);
PrintWriter p=new PrintWriter(s.getOutputStream());
BufferedReader in=new BufferedReader(new
InputStreamReader(s.getInputStream()));
BufferedReader ink=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("How many numbers to sort? ");
int num=Integer.parseInt(ink.readLine());
p.println(num);
p.flush();
System.out.println("Enter "+num+" numbers to sort :");
String sarr[]=new String[num];
for(int i=0;i<num;i++)
{
System.out.print("no. "+i+"=");
sarr[i]=ink.readLine();
p.println(sarr[i]);
p.flush();
}
String res;
System.out.println("\nSorted array::\n");
while((res=in.readLine())!=null)
{
System.out.println(res);
}
s.close();
}
}
Output:
Implement concurrent TCP server programming in which more
than one client can connect and communication with server
for sending the string and server returns the reverse of
string to each of client.
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
class myclient
{
public static void main(String args[])throws Exception
{
Socket s=new Socket("localhost",6666);
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();
}
}
Server.java
import java.io.*;
import java.net.*;
class myserver extends Thread
{
Socket s;
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(6666);
while(true)
{
Socket s=ss.accept();
server tt=new server(s);
Thread t=new Thread(tt);
t.start();
}
}
public void 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:
C:\Users\aj>cd Desktop
abc
cba
C:\Users\aj>cd Desktop