我做了一个模拟QQ聊天功能的东东;可是现在连连接到一起都成问题,老是出错有没有高手能帮忙看看啊,怎么样让他们连接在一起啊??
其中服务器代码如下:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Server extends JFrame
{
static JTextArea ja1 = new JTextArea(12,35);//静态变量
JTextArea ja2 = new JTextArea(5,12);
JScrollPane jp1 = new JScrollPane(ja1);
JScrollPane jp2 = new JScrollPane(ja2);
JButton jb1 = new JButton("发送");
JButton jb2 = new JButton("打开服务器");
String str1= "";
ServerSocket serverSocket;
DataInputStream inFromClient;
DataOutputStream outToClient;
Socket connectToClient;
public Server()
{
super("服务器");
Container con = getContentPane();
con.setLayout(new FlowLayout());
jb1.addActionListener(new ActionListener()
{
//按键jb1触发事件
public void actionPerformed(ActionEvent e)
{
Connect(); //调用连接函数
ja2.setText("连接到客户端");
}
});
jb2.addActionListener(new ActionListener()
{
//按键jb2触发事件
public void actionPerformed(ActionEvent e)
{
sentData();//调用发送数据函数
}
});
con.add(jp1);
con.add(jp2);
con.add(jb1);
con.add(jb2);
setBounds(100,100,400,400);
setVisible(true);
} ////////////////////////////////完成组件构造
public void Connect()//连接主机
{
try{
serverSocket = new ServerSocket(5500);
connectToClient = serverSocket.accept();
}
catch(IOException e)
{
e.getStackTrace();
}
}
public void sentData()//发送数据
{
try
{
//inFromClient = new DataInputStream(connectToServer.getInputStream());
outToClient = new DataOutputStream(connectToClient.getOutputStream());
outToClient.writeUTF(str1);
outToClient.flush();
}
catch(IOException e)
{
}
}
public static void main(String[]args)
{
Server cp = new Server();
cp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
客户端代码如下:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Client extends JFrame
{
static JTextArea ja1 = new JTextArea(12,35);//静态变量
JTextArea ja2 = new JTextArea(5,12);
JScrollPane jp1 = new JScrollPane(ja1);
JScrollPane jp2 = new JScrollPane(ja2);
JButton jb1 = new JButton("发送");
JButton jb2 = new JButton("连接");
String str1= "";
Socket connectToServer;
DataInputStream inFromServer;
DataOutputStream outToServer;
public Client()
{
super("客户端");
Container con = getContentPane();
con.setLayout(new FlowLayout());
jb1.addActionListener(new ActionListener()
{
//按键jb1触发事件
public void actionPerformed(ActionEvent e)
{
Connect(); //调用连接函数
ja2.setText("连接到服务器");
}
});
jb2.addActionListener(new ActionListener()
{
//按键jb2触发事件
public void actionPerformed(ActionEvent e)
{
sentData();//调用发送数据函数
}
});
con.add(jp1);
con.add(jp2);
con.add(jb1);
con.add(jb2);
setBounds(100,100,400,400);
setVisible(true);
} ////////////////////////////////完成组件构造
public void Connect()//连接主机
{
try{
connectToServer = new Socket("192.168.1.93",5500);
}
catch(IOException e)
{
e.getStackTrace();
}
}
public void sentData()//发送数据
{
try
{
//inFromServer = new DataInputStream(connectToServer.getInputStream());
outToServer = new DataOutputStream(connectToServer.getOutputStream());
outToServer.writeUTF(str1);
outToServer.flush();
}
catch(IOException e)
{
}
}
public static void main(String[]args)
{
Client cp = new Client();
cp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}