пример клиента на сокетах

 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
package ru.rpod.taop._16;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
try {
Socket s= new Socket("localhost", 3456);
System.out.println("Connected");
OutputStream os = s.getOutputStream();
String string = "Hello from client";
byte[] bytes = string.getBytes();
os.write(bytes);
System.out.println("client send information");
InputStream is = s.getInputStream();
is.read(bytes);
System.out.println("Get info: "+new String(bytes));
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}