Socket is the Java programming model to establish the communication link between two processes. A socket can hold input and output stream. A process sends data to another process through the network by writing to the output stream associated with the socket. A process reads data written the another process by reading from the input stream associated with the socket.
Code 5−7: The HostServer.java application
import java.net.*;
import java.io.*;
public class HostServer{
public static void main (String [ ] args){
ServerSocket s = null;
try{
s = new ServerSocket(5432);
Socket s1 = s.accept();
OutputStream s1out = s1.getOutputStream ();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter (s1out 12 bw.write (”Hellow Net World”);
bw.close();
s1.close();
}catch (IOException e){
e.printStackTrac e ();
}
}
}
import java.net.*;
import java.io.*;
public class ClientServer{
public static void main (String [ ] args){
try{
Socket s1 = new Socket(”127.0.0.1”, 5432);
InputStream is = s1.get InputSt ream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
dis.close();
s1.close();
}catch (ConnectException e1){
System.out.println(e1);
}catch(IOException e2){
e2.printStackTrace();
}
}
}
Code 5−7: The HostServer.java application
import java.net.*;
import java.io.*;
public class HostServer{
public static void main (String [ ] args){
ServerSocket s = null;
try{
s = new ServerSocket(5432);
Socket s1 = s.accept();
OutputStream s1out = s1.getOutputStream ();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter (s1out 12 bw.write (”Hellow Net World”);
bw.close();
s1.close();
}catch (IOException e){
e.printStackTrac e ();
}
}
}
import java.net.*;
import java.io.*;
public class ClientServer{
public static void main (String [ ] args){
try{
Socket s1 = new Socket(”127.0.0.1”, 5432);
InputStream is = s1.get InputSt ream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
dis.close();
s1.close();
}catch (ConnectException e1){
System.out.println(e1);
}catch(IOException e2){
e2.printStackTrace();
}
}
}
0 comments:
Post a Comment