java socket 客户端接收服务器发来的数据 只要客户端,服务器不用接收客户端数据就是发送数据
展开全部
class ClientChatThread extends JFrame implements Runnable{
boolean flag=true;
DataInputStream dis;
DataOutputStream dos;
Socket c;
JLabel lSend=new JLabel("请输入要发送的内容");
JTextField txtSendMessage=new JTextField(20);
JButton btnSend=new JButton("发送");
JTextArea txaContent=new JTextArea(10,20);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
public ClientChatThread(Socket c,int num) throws IOException{
super("客户端"+num);
this.c=c;
dis=new DataInputStream(c.getInputStream());
dos=new DataOutputStream(c.getOutputStream());
init();
}
private void init(){
setLayout(new BorderLayout());
p1.setLayout(new FlowLayout());
p1.add(lSend);p1.add(txtSendMessage);p1.add(btnSend);
this.add(p1,BorderLayout.NORTH);
p2.add(txaContent);
this.add(p2,BorderLayout.CENTER);
btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String tmp=txtSendMessage.getText().trim();
txaContent.setText(txaContent.getText()+"客户端说:"+tmp+"\n");
try {
dos.writeUTF(tmp);
txtSendMessage.setText("");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("客户端发送信息失败");
}
if(tmp.equals("bye"))
flag=false;
}
});
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(700,500);
this.setVisible(true);
}
public void run(){
String tmp=null;
while(flag){
try {
tmp=dis.readUTF().trim();
this.txaContent.setText(txaContent.getText()+"服务器说:"+tmp+"\n");
if(tmp.equals("bye"))
break;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("客户端读取信息失败");
flag=false;
}
}
}
}
public class ClientChat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Socket c1=new Socket("localhost",3000);
ClientChatThread cct1=new ClientChatThread(c1,1);
new Thread(cct1).start();
Socket c2=new Socket("localhost",3000);
ClientChatThread cct2=new ClientChatThread(c2,2);
new Thread(cct2).start();
Socket c3=new Socket("localhost",3000);
ClientChatThread cct3=new ClientChatThread(c3,3);
new Thread(cct3).start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("服务器名找不到");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("连接服务器端失败");
}
}
}
boolean flag=true;
DataInputStream dis;
DataOutputStream dos;
Socket c;
JLabel lSend=new JLabel("请输入要发送的内容");
JTextField txtSendMessage=new JTextField(20);
JButton btnSend=new JButton("发送");
JTextArea txaContent=new JTextArea(10,20);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
public ClientChatThread(Socket c,int num) throws IOException{
super("客户端"+num);
this.c=c;
dis=new DataInputStream(c.getInputStream());
dos=new DataOutputStream(c.getOutputStream());
init();
}
private void init(){
setLayout(new BorderLayout());
p1.setLayout(new FlowLayout());
p1.add(lSend);p1.add(txtSendMessage);p1.add(btnSend);
this.add(p1,BorderLayout.NORTH);
p2.add(txaContent);
this.add(p2,BorderLayout.CENTER);
btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String tmp=txtSendMessage.getText().trim();
txaContent.setText(txaContent.getText()+"客户端说:"+tmp+"\n");
try {
dos.writeUTF(tmp);
txtSendMessage.setText("");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("客户端发送信息失败");
}
if(tmp.equals("bye"))
flag=false;
}
});
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(700,500);
this.setVisible(true);
}
public void run(){
String tmp=null;
while(flag){
try {
tmp=dis.readUTF().trim();
this.txaContent.setText(txaContent.getText()+"服务器说:"+tmp+"\n");
if(tmp.equals("bye"))
break;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("客户端读取信息失败");
flag=false;
}
}
}
}
public class ClientChat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Socket c1=new Socket("localhost",3000);
ClientChatThread cct1=new ClientChatThread(c1,1);
new Thread(cct1).start();
Socket c2=new Socket("localhost",3000);
ClientChatThread cct2=new ClientChatThread(c2,2);
new Thread(cct2).start();
Socket c3=new Socket("localhost",3000);
ClientChatThread cct3=new ClientChatThread(c3,3);
new Thread(cct3).start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("服务器名找不到");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("连接服务器端失败");
}
}
}
追问
就需要客户端就可以了,服务器端不知道是什么语言写的,服务器502端口我客户端不需要做什么就是接收服务端发来的数据
追答
我写的就是客户端的代码,服务端的没贴上,这个main函数可以启动三个客户端,都连到一个服务端口上。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询