Practical : 1
Subject : Advanced Java
1. Program: Using
TCP
--------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public
static void main(String[] args) throws Exception
{
ServerSocketss=new
ServerSocket(7888);
Socket
s=ss.accept();
DataInputStream
din=new DataInputStream(s.getInputStream());
String
str;
str=din.readUTF();
System.out.println("Client:\t"+str);
DataOutputStreamdout=new
DataOutputStream(s.getOutputStream());
DataInputStreammsg=new
DataInputStream(System.in);
while(true)
{
str=din.readUTF();
System.out.print("Client:\t"+str);
System.out.print("Server:\t");
str=msg.readLine();
dout.writeUTF(str);
}
}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client
{
public
static void main(String[] args) throws Exception
{
Socket
s=new Socket("127.0.0.1",7888);
if(s.isConnected())
{
System.out.println("Connected
to server");
}
DataInputStreammsg=new
DataInputStream(System.in);
String
str="Start Chat..............................................................................................";
DataOutputStreamdout=new
DataOutputStream(s.getOutputStream());
dout.writeUTF(str);
System.out.println(str);
DataInputStream
din=new DataInputStream(s.getInputStream());
while(true)
{
System.out.print("Client:\t");
str=msg.readLine();
dout.writeUTF(str+"\n");
str=din.readUTF();
System.out.println("Server:\t"+str);
}
}
}
--------------------------------------------------------------------------------
Output:
Server
Client
2. Program: Using
UDP
--------------------------------------------------------------------------------
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);
}
}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
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:
Server
Client
Sign up here with your email
4 comments
Write commentsreadline() method is depricated fix it....
ReplyYea,but it still works in latest JDK.
Replyc o o l tutorial :)
Replythank you sir
Thanks for sharing information on how to create chatting application using either TCP or UDP protocol.
ReplyConversionConversion EmoticonEmoticon