但是我同学运行用户程序,我运行服务器程序,却连接不上,为什么呢?具体代码如下
服务器程序:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Text1 extends Thread
{
private Socket serversocket=null;
int i=0;
public Text1(Socket socket)
{
this.serversocket=socket;
}
public void run()
{
try
{
final Socket socket=serversocket;
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final PrintWriter out = new PrintWriter(socket.getOutputStream());
final BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
JFrame jf=new JFrame("这是服务器,正在与客户"+i+"对话");
Container container=jf.getContentPane();
jf.setVisible(true); //设置窗体为可视
jf.setLayout(null); //窗体布局为网格
jf.setSize(400,450);
jf.setLocation(200,100 );
jf.setBackground(Color.BLUE);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea a=new JTextArea("");
container.add(a);
a.setBounds(0, 0, 385, 360);
a.setBackground(Color.ORANGE);
JTextField b=new JTextField("");
container.add(b);
b.setBounds(0,360, 385, 50);
b.setBackground(Color.PINK);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s=b.getText();
out.println(s);
out.flush();
a.append("\r\n");
a.append("您自己: "+s);
if(s=="bye")
{
try
{
out.close();
in.close();
socket.close();
}
catch(Exception e1)
{}
}
}
});
String str=null;
while(str!="bye")
{
try
{
str=in.readLine();
}
catch(Exception e1)
{}
a.append("\r\n");
a.append("来自客户: "+str);
}
a.setText("对话结束!");
try
{
in.close();
out.close();
socket.close();
}
catch(Exception e1)
{}
}
catch(Exception e)
{}
}
public static void main(String args[]) throws IOException
{
ServerSocket server=null;
boolean Listening=true;
try
{
server=new ServerSocket(9999);
}
catch(IOException e)
{}
while(Listening)
{
new Text1(server.accept()).start();
}
}
}
用户程序:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.WindowConstants;
public class MyClient extends JFrame{
public static void main (String args[]) throws IOException
{
Socket socket=new Socket("59.46.65.121",9999);
final PrintWriter out=new PrintWriter(socket.getOutputStream());
final BufferedReader in=new BufferedReader(new InputStreamReader (socket.getInputStream()));
final BufferedReader sin =new BufferedReader(new InputStreamReader (System.in));
JFrame jf=new JFrame("这是用户02,正在与服务器对话");
Container container=jf.getContentPane();
jf.setVisible(true); //设置窗体为可视
jf.setLayout(null); //窗体布局为网格
jf.setSize(400,450);
jf.setLocation(200,100 );
jf.setBackground(Color.BLUE);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea a=new JTextArea("");
container.add(a);
a.setBounds(0, 0, 385, 360);
a.setBackground(Color.ORANGE);
JTextField b=new JTextField("");
container.add(b);
b.setBounds(0,360, 385, 50);
b.setBackground(Color.PINK);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s=b.getText();
out.println(s);
out.flush();
a.append("\r\n");
a.append("您自己: "+s);
if(s=="bye")
{
try
{
out.close();
in.close();
socket.close();
}
catch(Exception e1)
{}
}
}
});
String str=null;
while(str!="bye")
{
try
{
str=in.readLine();
}
catch(Exception e1)
{}
a.append("\r\n");
a.append("来自服务器: "+str);
}
a.setText("对话结束!");
try
{
in.close();
out.close();
socket.close();
}
catch(Exception e1)
{}
}
}
你的服务端根本就没有用accept去接受客户端socket
不写注释 真的不是个好习惯