socket怎样应用于界面?

renlei413326889 2008-01-12 12:20:24
请问有谁知道怎样将socket简单通信应用于界面?
在此跪谢!
...全文
147 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
renlei413326889 2008-04-02
  • 打赏
  • 举报
回复
终于结贴了!再一次谢谢你热心的帮助!
网络咖啡 2008-01-14
  • 打赏
  • 举报
回复
将接受的数据按照逻辑在界面上显示即可
bear1986 2008-01-14
  • 打赏
  • 举报
回复
呵呵,看到了,来顶你的帖子
lihaifeng0412 2008-01-13
  • 打赏
  • 举报
回复
路过转转
renlei413326889 2008-01-13
  • 打赏
  • 举报
回复
谢了!
yexiong 2008-01-12
  • 打赏
  • 举报
回复
receive 时另开一个Thread
juan123 2008-01-12
  • 打赏
  • 举报
回复
呵呵,这个可和跪谢没什么太大关系
只是感觉都是学习
知道就说了
我现在还有好多问题
没人给我解决
不知道怎么回事
newflypig 2008-01-12
  • 打赏
  • 举报
回复
跪谢就给2分,好有诚意哦
juan123 2008-01-12
  • 打赏
  • 举报
回复
这个是我这些天研究的问题
给你参考下吧
juan123 2008-01-12
  • 打赏
  • 举报
回复
package serverclient;

import java.net.*;
import java.io.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Aserver extends JFrame{
Socket s=null;
ServerSocket ss;
JLabel l1=new JLabel("PORT");
JTextField t1=new JTextField(8);
JTextArea t2=new JTextArea(6,15);
JTextField t3=new JTextField(20);
JButton b1=new JButton("监听");
JButton b2=new JButton("发送");
JPanel p=new JPanel();
DataInputStream in;
DataOutputStream out;
String s1="";

public Aserver()
{
add(p);
p.add(l1);
p.add(t1);
p.add(b1);
p.add(t2);
p.add(t3);
p.add(b2);
b1.addActionListener(new BListener());
b2.addActionListener(new BListener());
this.setSize(300,260);
this.setLocation(320,140);
this.setVisible(true);



}


public static void main(String[] args)
{
new Aserver();


}


class BListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int port=Integer.parseInt(t1.getText());
if(e.getActionCommand().equals("监听"))
try{

ss=new ServerSocket(port);
t2.setText("监听的端口:"+port+"\n");
new Thread(){
public void run(){
try{
s=ss.accept();
t2.append("连接成功! 本地IP:"+s.getRemoteSocketAddress()+"\n");
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
while((s1=in.readUTF())!=null){
t2.append("客户端说:"+s1+"\n");
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}.start();

System.out.println("连接成功");
}


catch(IOException ie)
{
ie.printStackTrace();

}
catch(NumberFormatException e1) {

e1.printStackTrace();
}

if(e.getActionCommand().equals("发送"))
try
{

out.writeUTF(t3.getText());

t2.append( "服务器说:"+t3.getText()+"\n");



System.out.println("发送成功");

}
catch(Exception ie)
{
ie.printStackTrace();
System.out.println("发送失败");

}

}
}
}





package serverclient;

import java.net.*;
import java.io.*;

import javax.swing.*;
//import java.awt.*;
import java.awt.event.*;
public class Aclient extends JFrame{
Socket s=null;
//ServerSocket ss;
JLabel l1=new JLabel("IP");
JLabel l2=new JLabel("PORT");
JTextField t1=new JTextField(8);
JTextField t2=new JTextField(6);
JTextArea t3=new JTextArea(6,15);
JTextField t4=new JTextField(20);
JButton b1=new JButton("连接");
JButton b2=new JButton("断开");
JButton b3=new JButton("发送");
JPanel p=new JPanel();
DataInputStream in;
DataOutputStream out;
String s1;
public Aclient()
{
add(p);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(b1);
p.add(b2);
p.add(t3);
p.add(t4);
p.add(b3);
b1.addActionListener(new BListener());
b2.addActionListener(new BListener());
b3.addActionListener(new BListener());
this.setSize(400,250);
this.setLocation(320,140);
this.setVisible(true);

}

class BListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ip=t1.getText();
int port=Integer.parseInt(t2.getText());

if(e.getActionCommand().equals("连接"))
try
{

s=new Socket(ip,port);
t3.setText("连接成功 ! 本地IP:"+s.getRemoteSocketAddress()+"\n");
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
new Thread(){
public void run(){
try{
while((s1=in.readUTF())!=null){
t3.append("服务器说:"+s1+"\n");
}
}
catch(IOException e){
e.printStackTrace();
}
}

}.start();
System.out.println("连接成功");
}
catch(IOException ie)
{
ie.printStackTrace();

}
catch (NumberFormatException e1) {

e1.printStackTrace();
}
if(e.getActionCommand().equals("断开"))
try
{
s.close();
System.out.println("断开连接");
t3.append("断开连接!\n");

}
catch(IOException ie)
{
ie.printStackTrace();

}


if(e.getActionCommand().equals("发送"))
try
{



out.writeUTF(t4.getText());


t3.append( "客户端说:"+t4.getText()+"\n");


System.out.println("发送成功");


}
catch(Exception ie)
{
ie.printStackTrace();
System.out.println("发送失败");
}

}
}



public static void main(String[] args)
{
new Aclient();

}
}



62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧