Network Programming Section 6
Network Programming Section 6
import java.io.*;
import java.net.*;
import java.util.*;
//Server
String hostname = "www.oreilly.com";
// Client
Socket s = new Socket("localhost",1286);
InputStream sIn = s.getInputStream();
DataInputStream socketDis = new DataInputStream(sIn);
String testString = new String(socketDis.readUTF());
System.out.println(testString);
socketDis.close();
sIn.close();
s.close();
}
}
Prepared by: Eng/ Basma Elshoky
1. Server Side
- Once a client connects, it obtains the output stream from the socket, wraps it with
`DataOutputStream`, and sends the string "Hello World!" to the client using `writeUTF()`
method.
2. Client Side:
- It creates a socket and connects to the server running on localhost (127.0.0.1) on port 1286
using `Socket` class.
- Then, it gets the input stream from the socket, wraps it with `DataInputStream`, and reads the
string sent by the server using `readUTF()` method.
- Comment a client. Then run the server code, it will wait for a client to connect.
- within running comment the server code, run a client code to connect.
- When you run the client code, it connects to the server.