Saturday, January 7, 2012

TCP Server side Code Java Part 1




Client Side Program

import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;

class client
{
Public static void main(String args[])
{
Socket sock=null;
DataInputStream dis=null;
PrintStream ps=null;
System.out.println(" Trying to connect");
try
{
// to get the ip address of the server by the name

InetAddress ip =InetAddress.getByName
("Hari.calsoftlabs.com");

// Connecting to the port 1025 declared
in the Serverclass
// Creates a socket with the server
bind to it.

sock= new Socket(ip,Server.PORT);
ps= new PrintStream(sock.getOutputStream());
ps.println(" Hi from client");
DataInputStream is = new
DataInputStream(sock.getInputStream());
System.out.println(is.readLine());

}
0catch(SocketException e)
{
System.out.println("SocketException " + e);
}
catch(IOException e)
{
System.out.println("IOException " + e);
}

// Finally closing the socket from
the client side

finally
{
try
{
sock.close();
}
catch(IOException ie)
{
System.out.println(" Close Error :" +
ie.getMessage());
}
}

}
}

0 comments:

Post a Comment

Tu comentario será moderado la primera vez que lo hagas al igual que si incluyes enlaces. A partir de ahi no ser necesario si usas los mismos datos y mantienes la cordura. No se publicarán insultos, difamaciones o faltas de respeto hacia los lectores y comentaristas de este blog.