-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketFunction.java
67 lines (60 loc) · 2.38 KB
/
SocketFunction.java
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
import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
class SocketFunction {
static Socket socket;
public void SocketConnection(String ip, int port) throws Exception {
socket = new Socket(ip, port);
}
public void SocketConnection() throws Exception {
try {
String ip = JOptionPane.showInputDialog("IP Address");
int port = Integer.parseInt(JOptionPane.showInputDialog("Port"));
socket = new Socket(ip, port);
} catch (UnknownHostException uhe) {
System.out.println("Host unknown: " + uhe.getMessage());
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
}
public String SocketReading() {
String socketRead="";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
socketRead = in.readLine();
//System.out.println(socketRead);
} catch (UnknownHostException uhe) {
System.out.println("Host unknown: " + uhe.getMessage());
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
return socketRead;
}
public void SocketSendingbyKbd(String string) {
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write(string);
out.newLine();
out.flush();
} catch (UnknownHostException uhe) {
System.out.println("Host unknown: " + uhe.getMessage());
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
}
public void SocketSending(String str) {
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
Scanner str1 = new Scanner(System.in);
out.write(str);
out.newLine();
out.flush();
} catch (UnknownHostException uhe) {
System.out.println("Host unknown: " + uhe.getMessage());
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
}
}