请高手给我一个socket和serversocket多线程通信的例子 老师让我们写一个类似与QQ斗地主的游戏先有个登陆界面,再有一个游戏大厅(要求有10张桌子),然后还有一个打牌界面这涉及到界面编程和网络编程我对网络编程一无所知请高手给我一个socket和serversocket多线程通信的例子,注释越多越好... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 建议你去读《Java网络协议内幕》这本书,google搜索可以下载 这个是以前写的一个简单的C/S聊天室客户端import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;import java.util.*;public class Client extends JFrame implements ActionListener{ final JTextArea txta; JTextField txtf; JPanel pl; JButton bt; BufferedReader br; DataOutputStream out; PrintStream ps; Container f=this.getContentPane(); public Client(){ f.setLayout(new BorderLayout()); txta=new JTextArea(); f.add(txta,BorderLayout.CENTER); txtf=new JTextField(20); bt=new JButton("发送"); pl=new JPanel(); pl.setLayout(new FlowLayout()); pl.add(txtf); pl.add(bt); bt.addActionListener(this); f.add(pl,BorderLayout.SOUTH); setTitle("信息发送端"); setSize(500,300); setVisible(true); run(); new Thread(){ {start();} public void run(){ while(true){ try{ txta.append("收到消息:"+br.readLine()+"\n"); }catch(Exception ex){} } } }; } public void actionPerformed(ActionEvent e){ if(e.getSource()==bt){ String msg=txtf.getText(); try{ ps.println(msg); txta.append("已经发送消息:"+msg+"\n"); //txta.append("收到消息:"+br.readLine()+"\n"); } catch(Exception ex){ txta.append(ex.toString()+'\n'); } } } public void run(){ try{ Socket sc=new Socket("127.0.0.1",8000); out=new DataOutputStream(sc.getOutputStream()); ps = new PrintStream(out); br = new BufferedReader(new InputStreamReader(new DataInputStream(sc.getInputStream()))); } catch(IOException ex){ txta.append(ex.toString()+'\n'); } } public static void main(String args[]) { Client myclient=new Client(); }}服务器import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;import java.util.*;class Conn extends Thread{ private JTextArea txt; private Socket st; private String msg = null; private BufferedReader br = null; private PrintStream ps; public Conn(Socket st,JTextArea txt){ this.st = st; this.txt = txt; start(); } public void run(){ try{ br = new BufferedReader(new InputStreamReader(new DataInputStream(st.getInputStream()))); ps = new PrintStream(new DataOutputStream(st.getOutputStream())); }catch(Exception e){ System.err.println("input failed"); } while(true){ try{ msg = br.readLine(); txt.append("从客户端收到信息"+msg+'\n'); Server.send(msg); }catch(Exception e){ System.err.println("connection closed"); break; } } } public void send(String msg){ ps.println(msg); }}public class Server extends JFrame{ private JTextArea txt; private ServerSocket ss; private static java.util.List<Conn> conns = new ArrayList<Conn>(); public Server(){ txt=new JTextArea(); this.setTitle("服务器"); this.setLayout(new BorderLayout()); this.add(new JScrollPane(txt),BorderLayout.CENTER); this.setSize(500,300); this.setVisible(true); run(); } public void run(){ try{ ss = new ServerSocket(8000); }catch(Exception e){ System.err.println("open socket failed"); } txt.append("信息接受时间是:"+new Date()+"\n"); while(true){ try{ Socket st=ss.accept(); conns.add(new Conn(st,txt)); } catch(IOException ex){ System.err.println(ex); } } } public static void send(String msg){ for(Conn c:conns) c.send(msg); } public static void main(String args[]){ Server myserver=new Server(); }} 自学编程从零基础 打算学JAVA方向,一直到安卓开发需要看那些书和视频?求高手指点几句 初学递归算法。 500大分!异构系统的事务处理 请问Java程序的开发环境是什么? 请大伙帮个忙,我用jcrertor编辑怎么不成功啊? 有JAVA applet好的电子书么? 请问JRE是什么意思?主要是做什么用的? 用JBuilder7编了一个JAVA程序不能对数据库进行写操作,为什么呢? 在java中怎样得到现在是时间戳转换成正型 有关ResultSet的问题,帮助解决问题者另外加分... 关于数组的问题 笔试时被忽悠
客户端import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;public class Client extends JFrame implements ActionListener
{
final JTextArea txta;
JTextField txtf;
JPanel pl;
JButton bt;
BufferedReader br;
DataOutputStream out;
PrintStream ps;
Container f=this.getContentPane();
public Client(){
f.setLayout(new BorderLayout());
txta=new JTextArea();
f.add(txta,BorderLayout.CENTER);
txtf=new JTextField(20);
bt=new JButton("发送");
pl=new JPanel();
pl.setLayout(new FlowLayout());
pl.add(txtf);
pl.add(bt);
bt.addActionListener(this);
f.add(pl,BorderLayout.SOUTH);
setTitle("信息发送端");
setSize(500,300);
setVisible(true);
run();
new Thread(){
{start();}
public void run(){
while(true){
try{
txta.append("收到消息:"+br.readLine()+"\n");
}catch(Exception ex){}
}
}
};
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt){
String msg=txtf.getText();
try{
ps.println(msg);
txta.append("已经发送消息:"+msg+"\n");
//txta.append("收到消息:"+br.readLine()+"\n");
}
catch(Exception ex){
txta.append(ex.toString()+'\n');
}
}
}
public void run(){
try{
Socket sc=new Socket("127.0.0.1",8000);
out=new DataOutputStream(sc.getOutputStream());
ps = new PrintStream(out);
br = new BufferedReader(new InputStreamReader(new DataInputStream(sc.getInputStream())));
}
catch(IOException ex){
txta.append(ex.toString()+'\n');
}
}
public static void main(String args[])
{
Client myclient=new Client();
}
}服务器import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;class Conn extends Thread{
private JTextArea txt;
private Socket st;
private String msg = null;
private BufferedReader br = null;
private PrintStream ps;
public Conn(Socket st,JTextArea txt){
this.st = st;
this.txt = txt;
start();
}
public void run(){
try{
br = new BufferedReader(new InputStreamReader(new DataInputStream(st.getInputStream())));
ps = new PrintStream(new DataOutputStream(st.getOutputStream()));
}catch(Exception e){
System.err.println("input failed");
}
while(true){
try{
msg = br.readLine();
txt.append("从客户端收到信息"+msg+'\n');
Server.send(msg);
}catch(Exception e){
System.err.println("connection closed");
break;
}
}
}
public void send(String msg){
ps.println(msg);
}
}
public class Server extends JFrame{
private JTextArea txt;
private ServerSocket ss;
private static java.util.List<Conn> conns = new ArrayList<Conn>();
public Server(){
txt=new JTextArea();
this.setTitle("服务器");
this.setLayout(new BorderLayout());
this.add(new JScrollPane(txt),BorderLayout.CENTER);
this.setSize(500,300);
this.setVisible(true);
run();
}
public void run(){
try{
ss = new ServerSocket(8000);
}catch(Exception e){
System.err.println("open socket failed");
}
txt.append("信息接受时间是:"+new Date()+"\n");
while(true){
try{
Socket st=ss.accept();
conns.add(new Conn(st,txt));
}
catch(IOException ex){
System.err.println(ex);
}
}
}
public static void send(String msg){
for(Conn c:conns)
c.send(msg);
}
public static void main(String args[]){
Server myserver=new Server();
}
}