TCP socket (Echo server & Client)
CLIENT PROGRAM:
import java.io.*;
import java.net.*;
public class tcpclient
{
public final static int DEFAULT_PORT=10001;
public static void main(String arg[])throws Exception
{
String str;
System.out.println("enterthe string to echo");
BufferedReader Stream=new BufferedReader(new InputStreamReader(System.in));
String hostname="127.0.0.1";
try
{
str=Stream.readLine();
System.out.println("connecting to host " +hostname);
Socket s= new Socket(hostname,DEFAULT_PORT);
PrintStream out= new PrintStream(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out.println(str);
String line=in.readLine();
while(line!=null)
{
System.out.println("received from server " +line);
line=in.readLine();
}
s.close();
}
catch(SocketException e)
{
System.out.println("socketerror "+e);
}
catch(UnknownHostException e)
{
System.err.println("invalid host");
}
catch(IOException e)
{
System.err.println("ioerror" +e);
}}}
SERVER PROGRAM:
import java.io.*;
import java.net.*;
import java.util.Date.*;
class tcpserver
{
public final static int DEFAULT_PORT=10001;
public static void main(String []args)throws Exception
{
int port=DEFAULT_PORT;
try
{
ServerSocket server=new ServerSocket(port);
Socket connection=null;
while(true)
{
try
{
connection=server.accept();
System.out.println("accept connection from "+connection.getPort());
OutputStreamWriter out=new OutputStreamWriter(connection.getOutputStream());
BufferedReader in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=in.readLine();
while(line==null)
line=in.readLine();
System.out.println(line + "was echoed");
out.write(line);
out.flush();
connection.close();
}
catch(IOException e)
{}
finally
{
try
{
if(connection!=null)
connection.close();
}
catch(IOException e)
{
}}}}
catch(IOException e)
{
System.err.println(e);
}}}
OUTPUT:
import java.io.*;
import java.net.*;
public class tcpclient
{
public final static int DEFAULT_PORT=10001;
public static void main(String arg[])throws Exception
{
String str;
System.out.println("enterthe string to echo");
BufferedReader Stream=new BufferedReader(new InputStreamReader(System.in));
String hostname="127.0.0.1";
try
{
str=Stream.readLine();
System.out.println("connecting to host " +hostname);
Socket s= new Socket(hostname,DEFAULT_PORT);
PrintStream out= new PrintStream(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out.println(str);
String line=in.readLine();
while(line!=null)
{
System.out.println("received from server " +line);
line=in.readLine();
}
s.close();
}
catch(SocketException e)
{
System.out.println("socketerror "+e);
}
catch(UnknownHostException e)
{
System.err.println("invalid host");
}
catch(IOException e)
{
System.err.println("ioerror" +e);
}}}
SERVER PROGRAM:
import java.io.*;
import java.net.*;
import java.util.Date.*;
class tcpserver
{
public final static int DEFAULT_PORT=10001;
public static void main(String []args)throws Exception
{
int port=DEFAULT_PORT;
try
{
ServerSocket server=new ServerSocket(port);
Socket connection=null;
while(true)
{
try
{
connection=server.accept();
System.out.println("accept connection from "+connection.getPort());
OutputStreamWriter out=new OutputStreamWriter(connection.getOutputStream());
BufferedReader in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=in.readLine();
while(line==null)
line=in.readLine();
System.out.println(line + "was echoed");
out.write(line);
out.flush();
connection.close();
}
catch(IOException e)
{}
finally
{
try
{
if(connection!=null)
connection.close();
}
catch(IOException e)
{
}}}}
catch(IOException e)
{
System.err.println(e);
}}}
OUTPUT:
pgm_1a.docx | |
File Size: | 62 kb |
File Type: | docx |