-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_client.java
More file actions
51 lines (43 loc) · 1.27 KB
/
Copy pathsocket_client.java
File metadata and controls
51 lines (43 loc) · 1.27 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
//java socket client example
import java.io.*;
import java.net.*;
public class socket_client
{
public static void main(String[] args) throws IOException
{
Socket s = new Socket();
String host = "www.google.com";
PrintWriter s_out = null;
BufferedReader s_in = null;
try
{
s.connect(new InetSocketAddress(host , 80));
System.out.println("Connected");
//writer for socket
s_out = new PrintWriter( s.getOutputStream(), true);
//reader for socket
s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
//Host not found
catch (UnknownHostException e)
{
System.err.println("Don't know about host : " + host);
System.exit(1);
}
//Send message to server
String message = "GET / HTTP/1.0\r\n\r\n";
s_out.println( message );
System.out.println("Message send");
//Get response from server
String response;
while ((response = s_in.readLine()) != null)
{
System.out.println( response );
}
//close the i/o streams
s_out.close();
s_in.close();
//close the socket
s.close();
}
}