===================== Client ====================
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[])
{
try
{
// 7 is a port number
// Opening a connection
Socket sck = new Socket ("localhost", 7);
// Handling Output Stream (Data to be sent to the server
OutputStream os = sck.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("hi server");
// Handling Input Stream (Data to be receieved from the server
InputStream is = sck.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str= dis.readLine();
System.out.println(str);
sck.close();
}
catch (Exception e)
{
System.out.println("System error ...");
}
}
}
============================== Server ===============================
package practical1;
import java.net.*;
import java.io.*;
public class Server {
public static void main (String args[])
{
try
{
// Listening at port number 7
ServerSocket ss = new ServerSocket(7);
// When Client sends the request to the server, it will accept
Socket sck = ss.accept();
// Handling Input stream (Data to be received from the client
InputStream is = sck.getInputStream();
DataInputStream dis = new DataInputStream(is);
String data = dis.readLine();
System.out.println(data);
// Handling Output stream (Replying to the Client
OutputStream os = sck.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("Hello client ...");
sck.close();
}
catch(Exception e)
{
System.out.println("Server error ...");
}
}
}
Comments
Post a Comment