-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPClient.java
More file actions
79 lines (73 loc) · 2.29 KB
/
TCPClient.java
File metadata and controls
79 lines (73 loc) · 2.29 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main (String args[]) {
// arguments supply message and hostname
// Check command line
if (args.length < 2) {
System.err.println("Usage : ");
System.err.println("java TCPClient <Message> <server>");
System.exit (1);
}
Socket s = null;
try{
int serverPort = 7896;
System.out.println("starting a new client socket");
s = new Socket(args[1], serverPort);
ObjectOutputStream out =new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream( s.getInputStream());
System.out.println("subscribing as: " + args[0]);
formatted_msg msg = new formatted_msg(args[0], "dummy");
formatted_msg.CTRL ctrl = formatted_msg.CTRL.SETUP;
msg.set_ctrl(ctrl);
// out.writeUTF(args[0]); // UTF is a string encoding see Sn. 4.4
System.out.println("sending " + msg);
out.writeObject(msg);
System.out.println("receiving response");
// String data = in.readUTF(); // read a line of data from the stream
msg = (formatted_msg) in.readObject();
System.out.println("Received: "+ msg) ;
Thread.sleep (5000);
}catch (UnknownHostException e){System.out.println("Socket:"+e.getMessage());
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
}catch (IOException e){System.out.println("readline:"+e.getMessage());
}catch (ClassNotFoundException e){System.out.println("readline:"+e.getMessage());
}catch (InterruptedException e){System.out.println("readline:"+e.getMessage());
}finally {
if(s!=null)
try {
s.close();
}
catch (IOException e){
System.out.println("close:"+e.getMessage());
}
}
Listen t = new Thread();
t.start();
}
}
class Listen extends Thread
{
ObjectInputStream in;
public Listen(){
this.start();
}
public void run(){
boolean stop=false;
while (stop==false)
{
try
{
formatted_msg msg = (formatted_msg) in.readObject();
System.out.println("Received: "+msg);
}
catch (IOException e)
{
System.out.println("exception: "+e.getMessage());
}
catch (ClassNotFoundException e){
System.out.println("exception: "+e.getMessage());
}
}
}
}