我正在使用java进行聊天应用,整个代码都可以,但数据不是从一个客户端转移到服务器
当我点击登录按钮时,它输入的名称不会进入服务器,也不会进入消息
plz建议一些方法
聊天client.java
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Color;
import java.util.Date;
public class ChattingClient extends JFrame implements ActionListener
{
JTextArea chat;
JTextField msg;
JButton send, exit, log, onlineuser;
boolean logflag=false, onlineflag=false;
OnlinePanel panel;
static public String name;
ServerSocket ss;
Socket s;
OutputStreamWriter out;
BufferedReader br;
JFrame frame;
public ChattingClient()
{
init();
}
public void init()
{
setLayout(null);
setSize(555,400);
setLocationRelativeTo(this);
chat=new JTextArea();
chat.setBounds(5,5,530,240);
chat.setEditable(false);
msg=new JTextField();
msg.setBounds(5,260,400,30);
send=new JButton("Send");
send.setBounds(405,260,70,30);
send.addActionListener(this);
onlineuser=new JButton(">>");
onlineuser.setBounds(480,260,50,30);
onlineuser.addActionListener(this);
log=new JButton("Login");
log.setBounds(150,310,95,30);
log.addActionListener(this);
exit=new JButton("Exit");
exit.setBounds(280,310,95,30);
exit.addActionListener(this);
add(chat);
add(msg);
add(send);
add(exit);
add(onlineuser);
add(log);
setVisible(true);
try
{
s=new Socket("localhost",786);
out=new OutputStreamWriter(s.getOutputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
while(true)
{
try
{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String value=br.readLine();
if (value.startsWith("came "))
{
OnlinePanel.list.add(br.readLine());
}
else if (value.startsWith("gone "))
{
OnlinePanel.list.remove(br.readLine());
}
else
{
if(br.readLine()!=null)
{
chat.append(br.readLine());
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String ar[])
{
ChattingClient s=new ChattingClient();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==exit)
{
System.exit(0);
}
else if (e.getSource()==log)
{
if (logflag==true)
{
log.setText("Login");
logflag=false;
try
{
out.write("$#$#123456789$#$#logoutname "+name);
out.write("$#$#123456789$#$#logoutaddress "+s.getInetAddress());
System.out.println("logout "+name+" "+s.getInetAddress());
}
catch (IOException e1)
{
e1.printStackTrace();
}
name=null;
}
else
{
name=JOptionPane.showInputDialog(this, "Enter Name", "Name", JOptionPane.INFORMATION_MESSAGE);
if (name!=null)
{
log.setText("Logout");
logflag=true;
try
{
out.write("$#$#987654321$#$#loginname "+name);
out.write("$#$#987654321$#$#loginaddress "+s.getInetAddress());
System.out.println("login "+name+" "+s.getInetAddress());
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
else if(e.getSource()==send)
{
if(name!=null)
{
try
{
out.write(name+": "+msg.getText());
System.out.println("msg: "+name+" "+msg.getText());
}
catch (IOException e1)
{
e1.printStackTrace();
}
msg.setText(null);
}
else
{`enter code here`
JOptionPane.showMessageDialog(this, "Login First", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
chattingserver.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
public class ChattingServer
{
public ChattingServer()
{
Thread thread=new Thread()
{
@Override
public void run()
{
super.run();
try
{
ServerSocket ss = new ServerSocket(786);
Socket s=ss.accept();
BufferedReader reader=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter (s.getOutputStream());
String input=null;
String login = null;
String logout = null;
String loginip=null;
String logoutip=null;
String msg = null;
while (true)
{
input=reader.readLine();
if(input.startsWith("$#$#123456789$#$#loginname "))
{
login="came "+input.substring(28);
System.out.println("login msg come "+login);
}
else if(input.startsWith("$#$#987654321$#$#logoutname "))
{
logout="gone "+input.substring(28);
System.out.println("logout msg come "+logout);
}
else if(input.startsWith("$#$#123456789$#$#loginaddress "))
{
loginip=input.substring(24);
System.out.println("loginip msg come "+loginip);
}
else if(input.startsWith("$#$#987654321$#$#logoutaddress "))
{
logoutip=input.substring(24);
System.out.println("logoutip msg come "+logoutip);
}
else if(input.startsWith("name: "))
{
msg=input.substring(7);
System.out.println("msg msg come "+msg);
}
if(login!=null)
{
out.write("\t\t\t"+login+"\t\t\t");
System.out.println("login msg goes "+login);
login=null;
}
if(logout!=null)
{
out.write("\t\t\t"+logout+"\t\t\t");
System.out.println("logout msg goes "+logout);
logout=null;
}
if(msg!=null)
{
out.write(msg);
System.out.println("msg msg goes "+msg);
msg=null;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
};
thread.start();
}
public static void main(String[] args)
{
new ChattingServer();
}
}
答案 0 :(得分:0)
您的服务器正在等待一个完整的行(一串字符后跟\n
),但您从未发送\n
,因此服务器会一直等待它。 write
不会自动添加一个;你需要自己添加它:
out.write("$#$#987654321$#$#loginname "+name + "\n");
out.write("$#$#987654321$#$#loginaddress "+s.getInetAddress() + "\n");
同样的:
out.write(name+": "+msg.getText()+"\n");