Skip to content

Commit e28d1b6

Browse files
committed
Java第二十六天
1 parent 181f45a commit e28d1b6

37 files changed

+1481
-0
lines changed

day26/homework/day26作业.docx

11.4 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>chat_socket_client</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Wed Oct 24 13:13:24 CST 2012
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.elient;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
import java.net.UnknownHostException;
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
14+
public class Client extends Thread{
15+
16+
public Socket c_socket ;
17+
private Client_chatFrame c_chatFrame;
18+
private Client_enterFrame c_enterFrame;
19+
private Client_singleFrame c_singleFrame;
20+
public DataInputStream dis = null;
21+
public DataOutputStream dos = null;
22+
private boolean flag_exit = false;
23+
24+
private int threadID;
25+
26+
public Map<String, Client_singleFrame> c_singleFrames;
27+
public List<String> username_online;
28+
public List<Integer> clientuserid;
29+
public String username = null;
30+
public String chat_re;
31+
//getter, setter方法
32+
public Client_chatFrame getC_chatFrame() {
33+
return c_chatFrame;
34+
}
35+
public Client_singleFrame getC_singlFrame() {
36+
return c_singleFrame;
37+
}
38+
public void setC_singlFrame(Client_singleFrame c_singlFrame) {
39+
this.c_singleFrame = c_singlFrame;
40+
}
41+
public void setC_chatFrame(Client_chatFrame c_chatFrame) {
42+
this.c_chatFrame = c_chatFrame;
43+
}
44+
public Client_enterFrame getC_enterFrame() {
45+
return c_enterFrame;
46+
}
47+
public void setC_enterFrame(Client_enterFrame c_enterFrame) {
48+
this.c_enterFrame = c_enterFrame;
49+
}
50+
51+
public int getThreadID() {
52+
return threadID;
53+
}
54+
public void setThreadID(int threadID) {
55+
this.threadID = threadID;
56+
}
57+
public Client(){
58+
c_singleFrames = new HashMap<String, Client_singleFrame>();
59+
username_online = new ArrayList<String>();
60+
clientuserid = new ArrayList<Integer>();
61+
// signlechatuse = new ArrayList<String>();
62+
}
63+
64+
public static void main(String[] args) {
65+
Client client = new Client();
66+
Client_enterFrame c_enterFrame = new Client_enterFrame(client);
67+
client.setC_enterFrame(c_enterFrame);
68+
c_enterFrame.setVisible(true);
69+
}
70+
71+
public String login(String username, String hostIp, String hostPort) {
72+
this.username = username;
73+
String login_mess = null;
74+
try {
75+
c_socket = new Socket(hostIp, Integer.parseInt(hostPort));
76+
} catch (NumberFormatException e) {
77+
login_mess = "连接的服务器端口号port为整数,取值范围为:1024<port<65535";
78+
return login_mess;
79+
} catch (UnknownHostException e) {
80+
login_mess = "主机地址错误";
81+
return login_mess;
82+
} catch (IOException e) {
83+
login_mess = "连接服务其失败,请稍后再试";
84+
return login_mess;
85+
}
86+
return "true";
87+
}
88+
public void showChatFrame(String username) {
89+
getDataInit();
90+
c_chatFrame = new Client_chatFrame(this,username);
91+
c_chatFrame.setVisible(true);
92+
flag_exit = true;
93+
this.start();
94+
95+
}
96+
private void getDataInit() {
97+
try {
98+
dis = new DataInputStream(c_socket.getInputStream());
99+
dos = new DataOutputStream(c_socket.getOutputStream());
100+
} catch (IOException e) {
101+
e.printStackTrace();
102+
}
103+
104+
}
105+
106+
public void run() {
107+
while(flag_exit){
108+
try {
109+
chat_re = dis.readUTF();
110+
} catch (IOException e) {
111+
flag_exit = false;
112+
if(!chat_re.contains("@serverexit")){
113+
chat_re = null;
114+
}
115+
}
116+
if(chat_re != null){
117+
if(chat_re.contains("@clientThread")){
118+
int local = chat_re.indexOf("@clientThread");
119+
setThreadID(Integer.parseInt(chat_re.substring(0, local)));
120+
try {
121+
dos.writeUTF(username + "@login" + getThreadID() + "@login");
122+
} catch (IOException e) {
123+
e.printStackTrace();
124+
}
125+
}else{
126+
if(chat_re.contains("@userlist")){
127+
c_chatFrame.setDisUsers(chat_re);
128+
}else{
129+
if(chat_re.contains("@chat")){
130+
c_chatFrame.setDisMess(chat_re);
131+
}else{
132+
if(chat_re.contains("@serverexit")){
133+
c_chatFrame.closeClient();
134+
}else{
135+
if(chat_re.contains("@single")){
136+
c_chatFrame.setSingleFrame(chat_re);
137+
}
138+
}
139+
}
140+
}
141+
}
142+
}
143+
}
144+
}
145+
public void transMess(String mess) {
146+
try {
147+
dos.writeUTF(username + "@chat" + getThreadID() + "@chat"+ mess + "@chat");
148+
} catch (IOException e) {
149+
e.printStackTrace();
150+
}
151+
}
152+
public void exitChat() {
153+
try {
154+
dos.writeUTF(username + "@exit" + getThreadID() + "@exit");
155+
flag_exit = false;
156+
System.exit(0);
157+
} catch (IOException e) {
158+
e.printStackTrace();
159+
}
160+
}
161+
public void exitLogin() {
162+
System.exit(0);
163+
}
164+
public void exitClient() {
165+
flag_exit = false;
166+
System.exit(0);
167+
}
168+
}

0 commit comments

Comments
 (0)