-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketClient.java
More file actions
36 lines (26 loc) · 1.01 KB
/
Copy pathSocketClient.java
File metadata and controls
36 lines (26 loc) · 1.01 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
import java.io.*;
import java.net.*;
public class SocketClient {
public String connectForOneMessage(String sIP, int iPort, String sMessage){
try(Socket oSocket = new Socket()){
// Connect to server.
oSocket.connect(new InetSocketAddress(sIP, iPort), 5000);
OutputStream output = oSocket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
// Send message to server.
writer.println(sMessage);
writer.flush();
InputStream input = oSocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String sReturn = reader.readLine();
oSocket.close();
return sReturn;
}
catch (Exception ex){
System.out.println("[client]: Client exception: " + ex.getMessage());
// Prints more details on the error
//ex.printStackTrace();
return null;
}
}
}