0% found this document useful (0 votes)
162 views4 pages

Experiment 1: PROGRAM (Socket Programming Using TCP)

This document describes programs for socket programming using TCP and UDP in Java. For TCP socket programming, it provides code for a server and client side that allows the client to select an arithmetic operation, enter two numbers, perform the operation, and display the result. For UDP socket programming, the server program receives input from a client, sends a greeting message back, and closes when the client enters "end". The client program allows the user to enter their name and displays the greeting response from the server.
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)
162 views4 pages

Experiment 1: PROGRAM (Socket Programming Using TCP)

This document describes programs for socket programming using TCP and UDP in Java. For TCP socket programming, it provides code for a server and client side that allows the client to select an arithmetic operation, enter two numbers, perform the operation, and display the result. For UDP socket programming, the server program receives input from a client, sends a greeting message back, and closes when the client enters "end". The client program allows the user to enter their name and displays the greeting response from the server.
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/ 4

lOMoARcPSD|9544459

OM SUNIL CHAVAN
3119050

EXPERIMENT 10

PROGRAM (Socket Programming using TCP)

Server Side: Client Side:


import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
class ServerSide class ClientSide
{ {
public static void main(String[] args) public static void main(String[] args)
throws Exception throws Exception
{ {
int choice,a,b,c=0; int ch=0,a,b,c;
ServerSocket ss = new Socket s = new
ServerSocket(1024); Socket("localhost",1024);
Socket s = ss.accept(); BufferedReader br = new
BufferedReader br = new BufferedReader(new
BufferedReader(new InputStreamReader(System.in));
InputStreamReader (s.getInputStream() PrintStream ps=new
) ); PrintStream(s.getOutputStream());
choice System.out.println("Please Enter
=Integer.parseInt(br.readLine()); Number 1:");
a =Integer.parseInt(br.readLine()); a = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine()); System.out.println("Please Enter
switch(choice) Number 2:");
{ b = Integer.parseInt(br.readLine());
case 1 : c = a+b; System.out.println("Please Enter The
break; Operation to Be
case 2 : c = a-b; Performed\n");
break; System.out.println("1.Addition
case 3 : c = a*b; 2.Subtraction
break; 3.Multiplication 4.Divison 5.Modulo
case 4 : c = a/b; 0.Exit"); ch =
break; Integer.parseInt(br.readLine());
case 5 : c = (a%b); ps.println(ch);
break; ps.println(a);
} ps.println(b);
PrintStream pr = new BufferedReader br1 = new
PrintStream(s.getOutputStream()) ; BufferedReader(new
pr.println(c); InputStreamReader(s.getInputStream()))
ss.close(); ;
s.close(); c=Integer.parseInt(br1.readLine());
}} System.out.println("Answer: "+c);
s.close();
}
}
lOMoARcPSD|9544459

OUTPUT:

Server side

Client side
lOMoARcPSD|9544459

PROGRAM (Socket Programming using UDP)

Server Socket Program: Client Socket Program:


import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.net.*; import java.net.*;
class ServerUDP class ClientUDP
{ {
public static void main(String public static void main(String
args[])throws IOException args[])throws IOException
{ {
DatagramSocket ss=new BufferedReader br=new
DatagramSocket(2100); BufferedReader(new
byte[] sendData=new byte[1024]; InputStreamReader(System.in));
byte[] recData=new byte[1024]; DatagramSocket ds=new
while(true) DatagramSocket();
{ InetAddress
DatagramPacket dp=new ip=InetAddress.getByName("localhost");
DatagramPacket(recData,recData.length) byte[] sendData=new byte[1024];
; byte[] recData=new byte[1024];
ss.receive(dp); System.out.println("Enter your name
String input=new String(dp.getData()); :");
if(input=="end") String input=br.readLine();
break; sendData=input.getBytes();
InetAddress ip=dp.getAddress(); DatagramPacket dp=new
System.out.println("Received input : DatagramPacket(sendData,sendData.lengt
"+input); h,ip,2100);
String output="Hello "; ds.send(dp);
int port=dp.getPort(); DatagramPacket dp1=new
output=output.concat(input); DatagramPacket(recData,recData.length)
sendData=output.getBytes(); ;
DatagramPacket dp1=new ds.receive(dp1);
DatagramPacket(sendData,sendData.lengt String output=new
h,ip,port); String(dp1.getData());
ss.send(dp1); System.out.println(output);
} ds.close();
ss.close(); }
}} }
lOMoARcPSD|9544459

OUTPUT:

Server side

Client side

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy