client和server连接问题

07-11-11 hongtaonn
请教高手 这个是服务器端

import java.io.*;

import java.io.IOException;

import java.net.*;

import java.awt.*;

public class ChatServer {

public static void main(String[] args) {

try {

ServerSocket ss= new ServerSocket(8883);

while(true){

Socket s = ss.accept();

System.out.print("a client connection");

DataInputStream dis = new DataInputStream(s.getInputStream());

String str = dis.readUTF();

System.out.print(str);

dis.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

这个是可户端import java.awt.*;

import java.awt.event.*;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.*;

import java.io.*;

public class ChatClient extends Frame {

Socket s = null;

TextField tfTex = new TextField();

TextArea tacontent = new TextArea();

public static void main(String[] args) {

new ChatClient().launchFrame();

}

public void launchFrame() {

setLocation(400, 300);

setSize(300, 300);

add(tfTex, BorderLayout.SOUTH);

add(tacontent, BorderLayout.NORTH);

pack();

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent arg0) {

System.exit(0);

}

});

tfTex.addActionListener(new TFListener());

setVisible(true);

connect();

}

public void connect() {

try {

s = new Socket("127.0.0.1", 8888);

System.out.print("connection");

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

private class TFListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

String str = tfTex.getText().trim();

tacontent.setText(str);

tfTex.setText("");

try {

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

dos.writeUTF(str);

dos.flush();

dos.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

为什么收不到可户端发的信息

连接上了以后貌似发的信息不显示服务器端也看不到撒 请高手们指点下谢谢

猜你喜欢