我尝试编写简单的 Messenger 看起来像书中的示例(Java 如何编程第 9 版 Paul Deitel Harvey Deitel,第 27.6 部分)。完成后,我尝试对其进行测试。它可以在本地主机(127.0.0.1)上工作,也可以在连接相同调制解调器的计算机上工作(我的意思是 ips 诸如 192.168.0.1 等)但是当我想在某些计算机上通过互联网测试它时,客户端代码出现连接被拒绝错误. 我想当我的客户(这是我在城市某处的朋友)尝试连接服务器(这是我,再次在城市的某个地方)时,当他输入我的 ip 来连接我时,他连接到我的调制解调器,而我的调制解调器不要'不要把它的信息发给我(很难用母语解释你无法深入理解的东西,所以我很抱歉)任何帮助对我来说都很重要。
这是服务器代码
//:D
//hello every Body:! this is my first program which use some networks !!! :-khatkeif
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.ServerSocket;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.EOFException;
public class Server extends JFrame
{
JTextField enteredText;//input message from user
JTextArea displayArea;//display information to user
ObjectInputStream input;//input object from cilent
ObjectOutputStream output;//output Object to client
ServerSocket server;//server Socket
Socket connection;//connecton to cilent
public Server()//constructor
{
super("Server");
setLayout(new BorderLayout(5,5));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel("Server Messenger"),BorderLayout.NORTH);
enteredText= new JTextField("entered message here");
enteredText.setEditable(false);
enteredText.addActionListener(new TextHandler());
add(enteredText,BorderLayout.SOUTH);
displayArea= new JTextArea();
displayArea.setEditable(false);
add(new JScrollPane(displayArea),BorderLayout.CENTER);
setSize(500,500);
setVisible(true);
}
public void runServer()
{
try
{
server = new ServerSocket(12345,10);//create server Socket
while(true)
{
waitForConnection();//wait util a client want to connect
openStreams();//open streams for send/get data
processConnection();//recive message from client
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
public void waitForConnection() throws IOException
{
displayMessage("waiting for client");
connection=server.accept();//alow server for connection
displayMessage("connect to "+connection.getInetAddress().getHostName());
}
public void openStreams() throws IOException//open stream that help me to send and recive message
{
displayMessage("setting I/O");
output= new ObjectOutputStream(connection.getOutputStream());//create output to send message
output.flush();//send headers to client
input = new ObjectInputStream(connection.getInputStream());//create input from client message
displayMessage("Got I/O");
}
public void processConnection() throws IOException//recive message from client & alow server to send message to client
{
try
{
displayMessage("connected successfully");
setTextEditable(true);//alow server to send message to client
String reciveMessage = (String)input.readObject();//recive message form client
while(!reciveMessage.equals("TERMINATE"))//if client send this string,make process connection finish
{
displayMessage(connection.getInetAddress().getHostName()+">>> "+reciveMessage);//display clinet message in display erea
reciveMessage=(String)input.readObject();//read next message
}
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
displayMessage(connection.getInetAddress().getHostName()+" disconnect`");
close();
}
public void close() throws IOException//close every thing
{
sendMessage("TERMINATE");
input.close();
output.close();
connection.close();
setTextEditable(false);
displayMessage("connection terminated");
}
public void setTextEditable(final boolean val)//set text field editable
{
SwingUtilities.invokeLater
(
new Runnable()
{
public void run()
{
enteredText.setEditable(val);
}
}
);
}
public void displayMessage(final String message)//display message in displayArea
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
displayArea.append(message+"\n");
}
}
);
}
public void sendMessage(String message) throws IOException//send message to client
{
output.writeObject(message);
output.flush();
displayMessage("Me>>> "+message);
enteredText.setText("");
}
private class TextHandler implements ActionListener//when user press enter a text in enteredText send to client
{
public void actionPerformed(ActionEvent ev)
{
try
{
sendMessage(ev.getActionCommand());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
这是主要的
//:D
public class ServerRun
{
public static void main(String[] args)
{
Server server = new Server();
server.runServer();
}
}
让我们去获取客户端代码:D
//:D
//this is client side
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.InetAddress;
public class Client extends JFrame
{
JTextField enteredText;//user input message
JTextArea displayArea;//display information
ObjectOutputStream output;//send message to server
ObjectInputStream input;//recive message from server;
Socket connection;//connection to server
String serverInfo;//server name
JButton closeB;
JPanel downPanel;
public Client ()
{
super("Client");
setLayout(new BorderLayout(5,5));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel("Client Messenger"),BorderLayout.NORTH);
enteredText= new JTextField("Enter message Here");
setTextEditable(false);
enteredText.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
try
{
sendMessage(ev.getActionCommand());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
);
closeB= new JButton("close");
closeB.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
try
{
close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
);
downPanel= new JPanel(new BorderLayout(5,5));
downPanel.add(enteredText,BorderLayout.CENTER);
downPanel.add(closeB,BorderLayout.EAST);
add(downPanel,BorderLayout.SOUTH);
displayArea= new JTextArea();
displayArea.setEditable(false);
add(new JScrollPane(displayArea),BorderLayout.CENTER);
setSize(500,500);
setVisible(true);
}
public void runClient(String host)//run client program
{
try
{
connectToServer(host);
openStreams();
processConnection();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
public void connectToServer(String host) throws IOException//connect to server
{
displayMessage("connceting to " + host);
connection = new Socket(InetAddress.getByName(host),12345);//create connection to server
}
public void openStreams() throws IOException // open streams for recive/send messages
{
output= new ObjectOutputStream( connection.getOutputStream());//create output for send messages
output.flush();//send headers
input = new ObjectInputStream(connection.getInputStream());//create input for recive messages
displayMessage("connected to "+connection.getInetAddress().getHostName());
}
public void processConnection()throws IOException //recive message util server trminate
{
try
{
setTextEditable(true);//alow user to send message
String reciveMessage=(String) input.readObject();
while(!reciveMessage.equals("TERMINATE"))
{
displayMessage(connection.getInetAddress().getHostName()+">>> "+reciveMessage);
reciveMessage=(String)input.readObject();
}
displayMessage("connection lost");
}
catch(ClassNotFoundException cnfe)
{
displayMessage("server message is not clear");
}
}
public void close() throws IOException//close every thing
{
sendMessage("TERMINATE");
input.close();
output.close();
connection.close();
System.exit(0);
}
public void sendMessage(String message) throws IOException//send message to server
{
output.writeObject(message);//send message to server
output.flush();//send headers
displayMessage("Me>>> "+message);//displate sent message to user
enteredText.setText("");
}
public void displayMessage(final String message)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
displayArea.append(message+"\n");
}
}
);
}
public void setTextEditable(final boolean val)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
enteredText.setEditable(val);
}
}
);
}
}
这是主要的:
//:D
import javax.swing.JOptionPane;
public class ClientRun
{
public static void main(String args[])
{
Client client = new Client();
client.runClient(JOptionPane.showInputDialog(null,"please enter host IP:"));
}
}
再次感谢