带 GUI 的 java ChatServer
我正在尝试使用 GUI 组件来实现聊天服务器。我实现了 3 个部分(服务器、客户端和 GUI 组件)。
以下是我遇到的一些问题 -
- 我无法用 GUI 代码包装代码。
- 当与客户交谈时,只有当每个人都只输入一行并按回车键时,它才有效。
代码:
第一个 GUI 组件:
public class ChatServer extends javax.swing.JFrame {
String str;
public ChatServer() {
initComponents();
screen.setEditable(false);
}
private void sendActionPerformed(java.awt.event.ActionEvent evt) {
str = enter.getText();
enter.setText("");
screen.append(str+"\n");
}
public static void main(String args[]) {
new ChatServer().setVisible(true);
}
private javax.swing.JTextPane enter;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea screen;
private javax.swing.JButton send;
}
,它看起来像:
这个我的服务器代码:
public class Server {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(23);
System.out.println(InetAddress.getLocalHost()+" hazir");
while(true){
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostName() + " baglandi");
new ServerPart(s).start();
}
}
}
public class ServerPart extends Thread {
private Socket s;
public ServerPart(Socket s){
this.s=s;
}
@Override
public void run() {
try {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Hello" + s.getInetAddress().getHostName());
String gelen;
while(true){
Scanner sc = new Scanner(s.getInputStream());
gelen = sc.nextLine();
if(gelen.trim().equalsIgnoreCase("bye"))
break;
System.out.println("Client: " + gelen);
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );
ps.println("Server: " + input.readLine());
}
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端代码:
public class Client extends Thread {
private Socket s;
public Client(Socket s){
this.s=s;
}
@Override
public void run() {
try {
PrintStream ps = new PrintStream(s.getOutputStream());
Scanner sc = new Scanner(s.getInputStream());
ps.println("Hello" + s.getInetAddress().getHostName());
String gelen;
while(true){
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );
gelen = sc.nextLine();
if(gelen.trim().equalsIgnoreCase("bye"))
break;
System.out.println("Client: " + gelen);
ps.println("Server: " + input.readLine());
}
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("192.168.1.173", 23);
new Client(s);
}
}
如果您能帮助我,我将不胜感激。
I am trying to implement a chatserver with using GUI components. I implemented 3 parts(Server,Client and GUI components).
Following are a few problems I have -
- I can't wrap the code with GUI code.
- When talking with clients, it only works when everybody enter only one line and press enter.
Code:
First GUI component:
public class ChatServer extends javax.swing.JFrame {
String str;
public ChatServer() {
initComponents();
screen.setEditable(false);
}
private void sendActionPerformed(java.awt.event.ActionEvent evt) {
str = enter.getText();
enter.setText("");
screen.append(str+"\n");
}
public static void main(String args[]) {
new ChatServer().setVisible(true);
}
private javax.swing.JTextPane enter;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea screen;
private javax.swing.JButton send;
}
and it looks like:
This my server code:
public class Server {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(23);
System.out.println(InetAddress.getLocalHost()+" hazir");
while(true){
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostName() + " baglandi");
new ServerPart(s).start();
}
}
}
public class ServerPart extends Thread {
private Socket s;
public ServerPart(Socket s){
this.s=s;
}
@Override
public void run() {
try {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Hello" + s.getInetAddress().getHostName());
String gelen;
while(true){
Scanner sc = new Scanner(s.getInputStream());
gelen = sc.nextLine();
if(gelen.trim().equalsIgnoreCase("bye"))
break;
System.out.println("Client: " + gelen);
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );
ps.println("Server: " + input.readLine());
}
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client code:
public class Client extends Thread {
private Socket s;
public Client(Socket s){
this.s=s;
}
@Override
public void run() {
try {
PrintStream ps = new PrintStream(s.getOutputStream());
Scanner sc = new Scanner(s.getInputStream());
ps.println("Hello" + s.getInetAddress().getHostName());
String gelen;
while(true){
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );
gelen = sc.nextLine();
if(gelen.trim().equalsIgnoreCase("bye"))
break;
System.out.println("Client: " + gelen);
ps.println("Server: " + input.readLine());
}
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("192.168.1.173", 23);
new Client(s);
}
}
Would appreciate if you can help me mates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
PrintStream
类,以及方法ps.readline();
。如果您正在开发聊天应用程序,那么这种方法将不起作用,因为 readline 方法在找到换行符或文件结尾时,或者在回车即输入时终止流。所以我更喜欢使用.. datainputstream 和 dataoutputstream --You are using
PrintStream
class , with methodps.readline();
. If you are developing a chat application, then this approach will not work, as readline method terminate the stream when found newline or End of file, or when carriage return i.e. enter. So I do prefer to use.. datainputstream and dataoutputstream --