저는이 책에서 예제처럼 간단한 메신저 모양을 쓰려고합니다 (Java How To 프로그램 9th Edition Paul Deitel Harvey Deitel, 27.6). 내가 끝내면, 나는 그것을 시험하려고 노력한다. localhost (127.0.0.1)에서 작동하며 동일한 모뎀 (iips 같은 192.168.0.1 등)을 연결하는 컴퓨터에서도 작동하지만 일부 컴퓨터에서 인터넷에서 클라이언트 측 코드를 테스트하려고 할 때 연결이 거부되었습니다. . 내가 클라이언트 (내 도시의 어딘가에있는 내 친구)가 서버를 연결하려고 할 때 (내 도시에있는 곳), 내 IP를 입력하면 저의 모뎀과 모뎀에 연결됩니다. 나에게 정보를 보내지 마라. (아무리 모국어로도 이해할 수없는 것을 설명하기가 어렵다. 그래서 나는 전혀 미안하다.) 어떤 도움이 내게 중요하다. 여기 간단한 서버/클라이언트 프로그램 오류가 발생합니다. java.net.ConnectException : 연결이 거부되었습니다.
는 서버 코드//: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:"));
}
}
다시 감사합니다.
thanks :) 매우 도움이됩니다. – destrat18