-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClient.java
More file actions
46 lines (31 loc) · 1.18 KB
/
SocketClient.java
File metadata and controls
46 lines (31 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class SocketClient {
public void processSocket(String serverName, int portNumber) {
try{
System.out.println("Connecting to " + serverName + " : " + portNumber);
Socket client = new Socket(serverName, portNumber);
System.out.println("Connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello From " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF() );
client.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SocketClient socketClient1 = new SocketClient();
socketClient1.processSocket("localhost", 7777);
SocketClient socketClient2 = new SocketClient();
socketClient2.processSocket("localhost", 8888);
}
}