0 / 0 / 0
Регистрация: 12.10.2015
Сообщений: 9
|
|
1
|
Не отображаются ники при отправке сообщений в чате чата
03.10.2016, 02:39. Показов 887. Ответов 1
В чате при отправке сообщений не отображаются ники(подскажите в чём ошибка?
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
163
164
165
| import java.net.*;
import java.util.Scanner;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class FieldWindow extends Frame implements ActionListener
{
private static final long serialVersionUID = 1L;
static TextArea chat=new TextArea("",25,65,TextArea.SCROLLBARS_VERTICAL_ONLY);
TextField str=new TextField(58);
Button send=new Button("Send");
Button ext=new Button("Exit");
static String command="";
static String incopy=null;
static String mess=null;
Frame wind1=new Frame("Chat");
static int len,i=0,u=0;
FieldWindow()
{
wind1.setLayout(new FlowLayout());
wind1.add(chat);
wind1.add(str);
wind1.add(send);
ext.addActionListener(this);
send.addActionListener(this);
wind1.add(ext);
wind1.setSize(500,500);
wind1.setLocation(120,60);
wind1.setResizable(false);
wind1.setVisible(true);
}
public void start()
{
if(NetClientThread.Errors!=null)
{
chat.append(NetClientThread.Errors);
NetClientThread.Errors=null;
}
while(true)
{
if((NetClientThread.Message!=null)&&(NetClientThread.Message!=""))
{
incopy=NetClientThread.Message;
NetClientThread.Message=null;
switch(incopy.charAt(0))
{
case '@':
len=incopy.length()-1;
for(i=1;i<=len;i++)
{
command=command+incopy.charAt(i);
}
i=1;
chat.append("В чат вошел пользаватель: "+command+"\n");
incopy=null;
command="";
break;
case '#':
len=incopy.length()-1;
for(i=1;i<=len;i++)
{
command=command+incopy.charAt(i);
}
i=1;
chat.append("Из чата вышел пользаватель: "+command+"\n");
incopy=null;
command="";
break;
default:
chat.append(incopy);
chat.append("\n");
}
}
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ext)
{
NetClientThread.out.close();
try
{
NetClientThread.in.close();
}
catch (IOException e2)
{
e2.printStackTrace();
}
try
{
NetClientThread.s.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
System.exit(0);
}
if(e.getSource()==send)
{
mess=str.getText();
NetClientThread.send(mess);
str.setText("");
}
}
}
public class NetClientThread extends Thread
{
Scanner scanner1 = new Scanner(System.in);
String nick1=scanner1.nextLine();
static String Errors=null;
static String Message=null;
static BufferedReader in = null;
static PrintWriter out;
static Socket s = null;
static String msg =null;
public NetClientThread()
{
try
{
s = new Socket("127.0.0.1", 8030);
InputStreamReader isr = new InputStreamReader (s.getInputStream());
in = new BufferedReader(isr);
out=new PrintWriter(s.getOutputStream(), true);
} catch (IOException e) {Errors="Ошибка соединения";}
}
public static void main(String[] args)
{
NetClientThread nct = new NetClientThread();
nct.start();
FieldWindow newwindow=new FieldWindow();
newwindow.start();
System.out.println("Введите ник");
}
public void run()
{
send("@" + nick1 );
while (true)
{
try
{
Message = in.readLine();
}
catch (IOException e) {break;}
}
FieldWindow.chat.append("Ошибка соединения с сервером");
}
public static void send(String msg)
{
out.println(msg);
out.flush();
}
} |
|
0
|