Java에서 다중 스레드 서버 채팅 응용 프로그램을 구현하려고합니다.
이 프로그램은 스레드를 생성하고 클라이언트가 연결될 때까지 기다립니다. 클라이언트가 연결되면 다른 스레드를 생성하고 다른 클라이언트가 연결되기를 기다립니다.다중 스레드 Java 서버 채팅 응용 프로그램 만들기.
이 내 ChatServer.java
package com.chat.server;
import java.io.InputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.InetAddress;
import java.net.ServerSocket;
/**
* <p>The chat server program</p>
* This class is a Thread that recieves connection
* from different clients and handles them is separate
* Thread.
*
* @author Aditya R.Singh
* @version 1.0.0
* @since 1.0.0
*/
class ChatServer extends Thread {
private int port; // The port number to listen at.
private String ip; // To store the IP address.
private Socket socket; // Socket connection with different clients.
private InetAddress inet; // Handling Client Address.
private ServerSocket serverSocket; // The server socket used by clients to connect.
/**
* This is solely intended for instantiation purpose.
*
* @param PORT - The port number to listen for client requests
*/
ChatServer(final int PORT) {
/* Initiallizing all instance variables to null. */
ip = null;
inet = null;
socket = null;
serverSocket = null;
/* Initiallizing the port number. */
port = PORT;
}
/**
* This method creates a connection between server and client.
*
* @throws java.io.IOException
*/
private void createConnection() throws IOException {
serverSocket = new ServerSocket(port); // Listen to the required port.
socket = serverSocket.accept(); // Accept the client connection.
}
/**
* This method sets the IP address.
*/
private void setIP() {
inet = socket.getInetAddress();
ip = new String(inet.getHostAddress());
}
/**
* This method returns the IP address.
*
* @return IP address.
*/
public String getIP() {
return ip;
}
/**
* This method checks if the socket has been connected
* with any client.
*
* @return True if the client has been connected, else false
*/
public boolean isConnected() {
if(socket == null)
return false;
return true;
}
/**
* This method returns the InputStream
* from the Socket.
*
* @return InputStream if Socket has been connected to the client, else null
* @see java.io.InputStream
*/
public InputStream getInputStream() throws IOException {
if(socket == null)
return null;
return socket.getInputStream();
}
@Override
public void run() {
try {
createConnection();
setIP();
} catch(IOException exception) {
exception.printStackTrace();
}
}
}
입니다 그리고 이것은 내 Server.java
입니다 :
package com.chat.server;
/**
* <p>The Server app</p>
* This is the controller for accepting connections.
*
* @author Aditya R.Singh
* @version 1.0.0
* @since 1.0.0
*/
public class Server {
/**
* The port at which clients will connect.
*/
public static final int PORT = 6005;
/**
* For instantiation purpose.
*/
public Server() {
}
public static void main(String[] args) {
/* Keep accepting connections. */
while(true) {
ChatServer chat = new ChatServer(PORT); // Connecting port.
chat.start();
while(!chat.isConnected())
/* This is a false loop. Intended to keep running unless another client is not requesting to connect. */;
System.out.println("We connected to: "+chat.getIP());
}
}
}
코드는 잘 컴파일합니다. 같은 코드를 실행에
는 :
java com.chat.server.Server
는 클라이언트가 연결하는 프로그램이 듣는 것 같다. 그러나 클라이언트에 연결 한 후에는 클라이언트의 IP 주소를 인쇄 한 다음 다른 클라이언트에 대해 다른 스레드를 작성해야합니다. 그러나 클라이언트의 IP를 인쇄하지는 않습니다. Socket connected
를 인쇄해야합니다,
package com.chat.client;
import java.net.Socket;
import java.io.IOException;
public class Client {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 6005);
System.out.println("Socket connected.");
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
클라이언트들 서버에 연결 :
이 내 Client.java
입니다. 클라이언트가 그렇게합니다. 클라이언트가 정상적으로 작동합니다.
java com.chat.client.Client
Socket connected.
그러나 서버 응용 프로그램은 클라이언트의 IP 주소를 인쇄하지 않습니다. 왜 그렇게?
이 [코드] (http://stackoverflow.com/a/25778305/3857942) 당신에게 채팅 서버를 수행하는 방법의 아이디어를 줄 수있는 완벽한 코드가 아닙니다. –