-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
52 lines (43 loc) · 1.16 KB
/
Client.java
File metadata and controls
52 lines (43 loc) · 1.16 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
47
48
49
50
51
52
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
public class Client{
private Socket server;
public Client(){
try{
server = new Socket("127.0.0.1", 12345);
}
catch(IOException ioe)
{
System.out.println("IOException " + ioe.getMessage());
}
}
public void communicate(){
try{
ObjectOutputStream output = new ObjectOutputStream(server.getOutputStream());
output.flush();
ObjectInputStream input = new ObjectInputStream(server.getInputStream());
//Communicate
output.writeObject("Cole");
output.flush();
String response = (String)input.readObject();
System.out.println("From server>> " + response);
//close connection
output.close();
input.close();
server.close();
}
catch(IOException ioe){
System.out.println("IO Exception: " + ioe.getMessage());
}
catch (ClassNotFoundException cnfe)
{
System.out.println("Class not found: " + cnfe.getMessage());
}
}
public static void main(String[] args){
Client client = new Client();
client.communicate();
}
}