0% found this document useful (0 votes)
327 views9 pages

Java Socket Programming Guide

The document contains code for client-server programs in Java using sockets for communication. It includes code for a date client and date server that exchange the current date, TCP client and server examples that allow for message passing, and an echo client and server where the client sends a message and the server returns the same message. The clients and servers create sockets and use input and output streams for sending and receiving data.

Uploaded by

sdkthemikado
Copyright
© Attribution Non-Commercial (BY-NC)
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)
327 views9 pages

Java Socket Programming Guide

The document contains code for client-server programs in Java using sockets for communication. It includes code for a date client and date server that exchange the current date, TCP client and server examples that allow for message passing, and an echo client and server where the client sends a message and the server returns the same message. The clients and servers create sockets and use input and output streams for sending and receiving data.

Uploaded by

sdkthemikado
Copyright
© Attribution Non-Commercial (BY-NC)
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

DATECLIENT

import [Link].*;
import [Link].*;
class dateclient
{
public static void main (String args[])
{
Socket soc;
DataInputStream dis;
String sdate;
PrintStream ps;
try
{
InetAddress ia=[Link]();
soc=new Socket(ia,8020);
dis=new DataInputStream([Link]());
sdate=[Link]();
[Link]("THE date in the server is:"+sdate);
ps=new PrintStream([Link]());
[Link](ia);
}
catch(IOException e)
{
[Link]("THE EXCEPTION is :"+e);

}
}
}
DATESERVER

import [Link].*;
import [Link].*;
import [Link].*;
class dateserver
{
public static void main(String args[])
{
ServerSocket ss;
Socket s;
PrintStream ps;
DataInputStream dis;
String inet;
try
{
ss=new ServerSocket(8020);
while(true)
{
s=[Link]();
ps=new PrintStream([Link]());
Date d=new Date();
[Link](d);
dis=new DataInputStream([Link]());
inet=[Link]();
[Link]("THE CLIENT SYSTEM ADDRESS IS :"+inet);
[Link]();
}
}
catch(IOException e)
{
[Link]("The exception is :"+e);
}
}
}
OUTPUT:

CLIENTSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java dateclient
THE date in the server is:wed dec22 14 :22:35 IST2010
C:\Program Files\Java\jdk1.5.0\bin>

SERVERSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java dateserver
THE CLIENT SYSTEM ADDRESS IS :skec-cscl1-39/[Link]
TCPSERVER
import [Link].*;
import [Link].*;
public class TCPserver
{
public static void main(String arg[])
{
ServerSocket s=null;
String line;
DataInputStream is=null,is1=null;
PrintStream os=null;
Socket c=null;
try
{
s=new ServerSocket(9999);
}
catch(IOException e)
{
[Link](e);
}
try
{
c=[Link]();
is=new DataInputStream([Link]());
is1=new DataInputStream([Link]);
os=new PrintStream([Link]());
do
{
line=[Link]();
[Link]("Client:"+line);
[Link]("Server:");
line=[Link]();
[Link](line);
}while([Link]("quit")==false);
[Link]();
[Link]();
}
catch(IOException e)
{
[Link](e);
}
}
}
TCPCLIENT
import [Link].*;
import [Link].*;
public class TCPclient1
{
public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("[Link]",9999);
}
catch(IOException e)
{
[Link](e);
}
try
{
os=new PrintStream([Link]());
is=new DataInputStream([Link]);
is1=new DataInputStream([Link]());
do
{
[Link]("Client:");
line=[Link]();
[Link](line);
[Link]("Server:" + [Link]());
}while([Link]("quit")==false);
[Link]();
[Link]();

}
catch(IOException e)
{
[Link]("Socket Closed!Message Passing is over");
}
}
}
OUT PUT :
SERVER
C:\Program Files\Java\jdk1.5.0\bin>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java TCPserver1
Client: Hai Server
Server:
Hai Client
Client: How are you
Server:
Fine
Client: quit
Server:
Quit
CLIENT

C:\Program Files\Java\jdk1.5.0\bin>javac [Link]


Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java TCPclient1
Client:
Hai Server
Server: Hai Client
Client:
How are you
Server: Fine
Client:
Quit
Server: quit
ESERVER
import [Link].*;
import [Link].*;
public class EServer
{
public static void main(String args[])
{
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
{
s=new ServerSocket(9000);
}
catch(IOException e)
{
[Link](e);
}
try
{
c=[Link]();
is=new DataInputStream([Link]());
ps=new PrintStream([Link]());
while(true)
{
line=[Link]();
[Link](line);
}
}
catch(IOException e)
{
[Link](e);
}
}
}
ECLIENT
import [Link].*;
import [Link].*;
public class EClient
{
public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("[Link]",9000);
}
catch(IOException e)
{
[Link](e);
}
try
{
os=new PrintStream([Link]());
is=new DataInputStream([Link]);
is1=new DataInputStream([Link]());
while(true)
{
[Link]("Client:");
line=[Link]();
[Link](line);
[Link]("Server:" + [Link]());
}

}
catch(IOException e)
{
[Link]("Socket Closed!");
}
}
}
OUTPUT:

SERVER

C:\Program Files\Java\jdk1.5.0\bin>javac [Link]


Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EServer
-

CLIENT

C:\Program Files\Java\jdk1.5.0\bin>javac [Link]


Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EClient
Client:
Hai Server
Server:Hai Server
Client:
Hello
Server:Hello
Client:
end
Server:end
Client:
ds
Socket Closed!

You might also like