2 / 2 / 1
Регистрация: 04.12.2012
Сообщений: 202
|
|
1
|
Multi-thread chat
20.10.2014, 19:55. Показов 614. Ответов 0
Здравствуйте, подскажите пожалуйста как после того как юзер зайдет в чат под своим именем и паролем я задал как temasey;123 смог отправлять сообщения на сервер и общаться с другими юзерами
Java | 1
2
3
4
5
6
7
| public interface Opcode {
int CLIENT_USERNAME = 1;
int CLIENT_INVALID_USERNAME = 2;
int CLIENT_PASSWORD = 3;
int CLIENT_INVALID_PASSWORD = 4;
int CLIENT_CONNECTED = 5;
} |
|
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class ClientThread implements Runnable {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private String chatName;
private String password;
private Thread thread;
private volatile boolean isRunning = true;
private int opcode;
private static Map<String, String> userpass = new HashMap<String, String>();
static {
userpass.put("temasey", "123");
userpass.put("anna", "456");
}
public ClientThread(Socket socket) {
try {
this.socket = socket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
thread = new Thread(this);
thread.start();
} catch (IOException e) {
System.out.println(e);
}
}
public void run() {
try {
while (isRunning) {
if (!in.ready())
continue;
opcode = Integer.parseInt(in.readLine());
switch (opcode) {
case Opcode.CLIENT_USERNAME:
chatName = in.readLine();
System.out.println(chatName + " is reqesting to connect.");
boolean result1 = userpass.containsKey(chatName);
if (result1) {
System.out.println(chatName + " is a valid username.");
out.println(Opcode.CLIENT_PASSWORD);
} else {
System.out.println(chatName + " is a invalid username.");
out.println(Opcode.CLIENT_INVALID_USERNAME);
}
break;
case Opcode.CLIENT_PASSWORD:
password = in.readLine();
System.out.println(chatName + " is reqesting to connect having password "
+ password);
boolean result2 = userpass.get(chatName).equals(password);
if (result2) {
System.out.println(password + " is a valid password for username "
+ chatName);
out.println(Opcode.CLIENT_CONNECTED);
} else {
System.out.println(password + " is a invalid password for username "
+ chatName);
out.println(Opcode.CLIENT_INVALID_PASSWORD);
}
break;
}
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
System.out.println(e);
}
}
} |
|
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
| import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class LiveChatServer {
private static LinkedHashMap<String, ClientThread> clientInfo = new LinkedHashMap<String, ClientThread>();
private ServerSocket serverSocket;
public LiveChatServer() {
startServer();
}
public void startServer() {
String port = "1243";
try {
int portNo = Integer.valueOf(port);
serverSocket = new ServerSocket(portNo, 0, InetAddress.getLocalHost());
System.out.println(serverSocket);
System.out.println(serverSocket.getInetAddress().getHostName() + ":"
+ serverSocket.getLocalPort());
while (true) {
Socket socket = serverSocket.accept();
new ClientThread(socket);
}
} catch (IOException e) {
System.out.println("IO Exception:" + e);
System.exit(1);
} catch (NumberFormatException e) {
System.out.println("Number Format Exception:" + e);
System.exit(1);
}
}
public static HashMap<String, ClientThread> getClientInfo() {
return clientInfo;
}
public static void main(String args[]) {
new LiveChatServer();
}
} |
|
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
public class LiveChatClient {
private String chatName;
private String password;
private String serverAddress;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public LiveChatClient() {
initHostName();
runClient();
}
public void initHostName() {
try {
serverAddress = "Admin-ПК";
if (serverAddress == null)
System.exit(1);
serverAddress = serverAddress.trim();
if (serverAddress.length() == 0)
{
System.out.println("Server IP Address or Name can't be blank.");
initHostName();
return;
}
System.out.println("Trying to connect with server...\nServer IP Address:"
+ serverAddress);
InetAddress inetAddress = InetAddress.getByName(serverAddress);
if (!inetAddress.isReachable(60000))// 60 sec
{
System.out
.println("Error! Unable to connect with server.\nServer IP Address may be wrong.");
System.exit(1);
}
initPortNo();
} catch (SocketException e) {
System.out.println("Socket Exception:\n" + e);
initHostName();
return;
} catch (IOException e) {
initHostName();
return;
}
}
public void initPortNo() {
try {
String portNo = "1243";
portNo = portNo.trim();
if (portNo.length() == 0)
{
System.out.println("Server port No can't be blank.");
initPortNo();
return;
}
System.out.println("Trying to connect with server...\nServer Port No:" + portNo);
socket = new Socket(serverAddress, 1243);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
System.out.println("IO Exception:\n" + e);
initPortNo();
return;
}
}
public void sendChatName() throws IOException {
System.out.println("Enter user name:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
if (name == null)
System.exit(1);
chatName = name.trim();
if (name.length() == 0) {
System.out.println("Please enter user name.");
sendChatName();
return;
}
out.println(Opcode.CLIENT_USERNAME);
out.println(chatName);
}
public void sendPassword() throws IOException {
System.out.println("Enter password:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
if (name == null)
System.exit(1);
password = name.trim();
if (name.length() == 0) {
System.out.println("Please enter password.");
sendPassword();
return;
}
out.println(Opcode.CLIENT_PASSWORD);
out.println(password);
}
public void runClient() {
try {
sendChatName();
while (true) {
int opcode = Integer.parseInt(in.readLine());
switch (opcode) {
case Opcode.CLIENT_INVALID_USERNAME:
System.out.println(chatName + " is invalid user name. Try different one.");
sendChatName();
break;
case Opcode.CLIENT_PASSWORD:
sendPassword();
break;
case Opcode.CLIENT_INVALID_PASSWORD:
// this client is connecting
System.out.println(password + " is invalid password. Try different one.");
sendPassword();
break;
case Opcode.CLIENT_CONNECTED:
System.out.println(chatName + " is connected successfully.");
break;
}
}
} catch (IOException e) {
System.out.println("Client is closed...");
}
}
public static void main(String args[]) {
new LiveChatClient();
}
} |
|
0
|