//客户端
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.*;
import java.io.*;public class Client extends JFrame{
private Socket s;
private JButton send=new JButton("发送");
private JPanel jp1=new JPanel();
private JPanel jp2=new JPanel();
private JTextArea jta=new JTextArea(24,24);
private JTextField jtf=new JTextField(15);
private Message m=new Message();
private ObjectOutputStream oos;
private ObjectInputStream ois;
public Client()
{
super("Client");
try {
s=new Socket("219.229.153.184",8000);
oos=new ObjectOutputStream(s.getOutputStream());
ois=new ObjectInputStream(s.getInputStream());
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
System.out.println(1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jta.setEditable(false);
jp1.add(jta);
this.add(jp1,"Center");
jp2.add(jtf);
jp2.add(send);
//m.Set("1");
this.setLocation(300,300);
this.add(jp2,"South");
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
send.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
jta.append(jtf.getText()+'\n');
m.Set(jtf.getText());
System.out.println(m.Get());
jtf.setText("");
try {
oos.writeObject(m);
oos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//m.Set("2");
}
});
get g=new get(ois);
g.start();
}
class get extends Thread
{
private ObjectInputStream ois;
private Message m=new Message();
public get(ObjectInputStream i)
{
ois=i;
}
public void run()
{
while(true)
{
try {
m.Set(((Message)ois.readObject()).Get());
jta.append(m.Get()+'\n');
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
new Client();
}
}
//服务器端
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.*;public class Server {
private ServerSocket ss;
private Connect c1;
private Connect c2;
public Server()
{
try {
ss=new ServerSocket(8000);
Socket s1=ss.accept();
Socket s2=ss.accept();
c1=new Connect(s1,s2);
c2=new Connect(s2,s1);
c1.start();
c2.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Server();
}
}
class Connect extends Thread
{
Socket s1;
Socket s2;
private ObjectOutputStream oos;
private ObjectInputStream ois;
Message m=new Message();
public Connect(Socket x1,Socket x2)
{
s1=x1;
s2=x2;
try {
oos=new ObjectOutputStream(s1.getOutputStream());
ois=new ObjectInputStream(s2.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
while(true)
{
try {
m.Set(((Message)ois.readObject()).Get());
oos.writeObject(m);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
解决方案 »
- 大虾们!菜鸟求助!
- 求救:静态变量(是个集合)的值是放在栈里的还是堆里的啊?
- 用 JCreator调试的错误
- 我在用jdk1.6时在JBUILDER2005下运行程序没有问题,但部署后出现以下问题,急
- 请大家帮个忙!!!
- 请教关于JConnect连接Sybase数据库的问题
- 大家帮我看看什么问题啊,在线等,急!!!!!
- 【求助】如何计算时间差?
- java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained,这个错误怎样解决?
- cs(se)项目,jdk版本过低,需要升级问题
- throw到底是什么东东
- 我已经生成一个jar文件,如何自己改变该jar文件的图标?
public class Message implements java.io.Serializable{
private String str=new String(); public String Get()
{
return str;
} public void Set(String str)
{
this.str = str;
}
}
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.*;
import java.io.*;public class Client extends JFrame{
private Socket s;
private JButton send=new JButton("发送");
private JPanel jp1=new JPanel();
private JPanel jp2=new JPanel();
private JTextArea jta=new JTextArea(24,24);
private JTextField jtf=new JTextField(15);
private Message m=new Message();
private ObjectOutputStream oos;
private ObjectInputStream ois;
public Client()
{
super("Client");
try {
s=new Socket("127.0.0.1",8000);
oos=new ObjectOutputStream(s.getOutputStream());
ois=new ObjectInputStream(s.getInputStream());
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
System.out.println(1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jta.setEditable(false);
jp1.add(jta);
this.add(jp1,"Center");
jp2.add(jtf);
jp2.add(send);
//m.Set("1");
this.setLocation(300,300);
this.add(jp2,"South");
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
send.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{ jta.append(jtf.getText()+'\n');
m.Set(jtf.getText());
System.out.println("发送内容:"+m.Get());
jtf.setText("");
try {
Message mm = new Message();
mm.Set(m.Get());
oos.writeObject(mm);
oos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//m.Set("2");
}
});
get g=new get(ois);
g.start(); }
class get extends Thread
{
private ObjectInputStream ois;
private Message m=new Message();
public get(ObjectInputStream i)
{
ois=i;
}
public void run()
{
while(true)
{
try {
Object nn=ois.readObject();
if(nn!=null)
{
Message mm = (Message)nn;
jta.append(mm.Get()+'\n');
System.out.println("收到内容:"+mm.Get());
}else
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
new Client();
}
}
//服务器端
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.*;public class Server {
private ServerSocket ss;
private Connect c1;
private Connect c2; public Server()
{
try {
ss=new ServerSocket(8000);
Socket s1=ss.accept();
c1=new Connect(s1);
c1.start(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Server();
}
}
class Connect extends Thread
{
Socket s1;
Socket s2;
private ObjectOutputStream oos;
private ObjectInputStream ois;
Message m=new Message();
public Connect(Socket x1)
{
s1=x1;
try {
oos=new ObjectOutputStream(s1.getOutputStream());
ois=new ObjectInputStream(s1.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
while(true)
{
try {
Object nn=ois.readObject();
if(nn!=null)
{
Message mm = (Message)nn;
System.out.println("服务端收到的内容:"+mm.Get());
oos.writeObject(mm);
oos.flush();
}else
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
}