-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketProgramming4.java
More file actions
73 lines (54 loc) · 2.07 KB
/
SocketProgramming4.java
File metadata and controls
73 lines (54 loc) · 2.07 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
import java.*;
import java.io.*;
import java.net.*;
public class SocketProgramming4{
private static int PORT=4242;
private static InetAddress local;
public static class Server{
public static void main(String[] args) throws Exception{
Thread t=new Thread(new Runnable(){
@Override
public void run(){
try{
ServerSocket server=new ServerSocket(PORT);
Socket s=server.accept();
DataInputStream in = new DataInputStream(s.getInputStream());
while(true){
System.out.print(in.readUTF());
}
}catch(SocketException se){
System.out.println("Connection has been closed by the client");
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
}
});
t.start();
}
}
public static class Client{
public static void main(String[] args) throws Exception{
local=InetAddress.getByName("182.64.147.16");
System.out.println(local);
Thread t=new Thread(new Runnable(){
@Override
public void run(){
try{
Socket s=new Socket(local.getHostAddress(), PORT);
DataOutputStream out=new DataOutputStream(s.getOutputStream());
int s2;
while(true){
s2=System.in.read();
out.writeUTF(Character.toString((char)(s2)));
}
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
}
});
t.start();
}
}
}