socket连接上服务端但是无法传输数据。
为此写了个测试的服务端、一个纯代码客户端、一个窗口客户端。
这是服务端测试代码,只是展开连接接收数据的:
public class 客户端聊天测试 {
public static void main(String[] args) throws Exception{
ServerSocket server = new ServerSocket(10032);
Socket socket = server.accept();
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = "";
System.out.println("服务器模拟端已展开在10032端口");
while(!str.equals("exit!")) {
str = br.readLine();
System.out.println(str);
}
br.close();
in.close();
socket.close();
server.close();
}
}
下面是一个简单的连接客户端,只是发送数据的:
public class 消息发送测试 {
public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
Socket socket = new Socket("localhost", 10032);
OutputStream out = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
bw.write("hello world\n");
bw.write("你好");
bw.newLine();
bw.write("exit!");
bw.close();
out.close();
socket.close();
}
}
到此完全没有问题,BUT:
我试着做成窗体模式发送数据时却只能连接上服务端而不能发送数据过去,下面是界面
界面代码省略,我在界面的构造方法里添加了下面的代码(注:socket\out\bw均已经声明为成员变量):
try {
socket = new Socket("localhost", 10032);
out = socket.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(out));
bw.write("hellow");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
其按钮监视器的代码如下:
class jian implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
bw.write(txt.getText());
bw.newLine();
System.out.println(txt.getText());
txt.setText("");
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
逻辑来说应该没问题然而这运行结果就很惨淡:
无论怎么输入都不能接受到消息,在监视器里的那句println是输出来了,但是服务端根本没接收到数据,但是连接确实是建立起来了,求大神帮忙ing
界面的构造方法里,有没有把实现了ActionListener的类加到button上?
象这样:
sendButton.addActionListener(new jian());
我照着你的代码写了个。
public class SendMessageWindow {
private JFrame frame;
private JTextField msgText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SendMessageWindow window = new SendMessageWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SendMessageWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
msgText = new JTextField();
frame.getContentPane().add(msgText);
msgText.setColumns(10);
JButton sendButton = new JButton("发送");
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String msg = msgText.getText();
try {
sendMsg(msg);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
msgText.setText("");
}
});
frame.getContentPane().add(sendButton);
}
public void sendMsg(String msg) throws UnknownHostException, IOException {
Socket socket = new Socket("localhost", 8080);
OutputStream out = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
bw.write(msg);
bw.close();
out.close();
socket.close();
}
}
服务端可以收到消息。
感觉服务端和客户端的socket编程代码没有问题。