2012-02-01 2 views
1

나는 간단한 Java 채팅 서버에서 작업 중입니다. 서버가 완료되었으므로 이제 클라이언트를 연결하려고합니다. 지금까지는 서버 측에서 연결하지 않고 오류를주는 것이 아니라 네트워크 프로그래밍에 대해 많이 알지 못했지만 클라이언트는 java <ChatClient> <IP> <Port>을 입력하여 연결해야합니다. 제대로 작동하지 않는 것처럼 보입니다. 나는 텔넷에서 테스트했기 때문에 서버가 작동한다는 것을 알고있다. 누구든지 어떤 제안이 있습니까? 귀하의 클리에채팅 클라이언트가 서버에 연결되지 않음

서버에 출력

Server Started... 
Connection reset 
Exception in thread "main" java.net.SocketException: Connection reset 
at java.net.SocketInputStream.read(SocketInputStream.java:130) 
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:282) 
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:324) 
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:176) 
at java.io.InputStreamReader.read(InputStreamReader.java:184) 
at java.io.BufferedReader.fill(BufferedReader.java:153) 
at java.io.BufferedReader.readLine(BufferedReader.java:316) 
at java.io.BufferedReader.readLine(BufferedReader.java:379) 
at ChatServer$HandleClient.<init>(ChatServer.java:47) 
at ChatServer.process(ChatServer.java:18) 
at ChatServer.main(ChatServer.java:23) 

채팅 클라이언트

import java.io.*; 
import java.util.*; 
import java.net.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import static java.lang.System.out; 

public class ChatClient { 
PrintWriter output; 
BufferedReader input; 
Socket client; 

public ChatClient(String ip, int port) throws Exception { 
client = new Socket(ip,port); 
input = new BufferedReader(new InputStreamReader(client.getInputStream())) ; 
    output = new PrintWriter(client.getOutputStream(),true); 
input.readLine(); 
} 

public static void main(String args[]) { 
    try { 
    String ip= args[0]; 
    int port= Integer.parseInt(args[1]); 
     new ChatClient(ip,port); 
    } catch(Exception ex) { 
     out.println("Error --> " + ex.getMessage()); 
    } 

} // end of main 

// inner class for Messages Thread 
class MessagesThread extends Thread { 
    public void run() { 
     String line; 
     try { 
      while(true) { 
       line = input.readLine(); 

      } // end of while 
     } catch(Exception ex) {} 
    } 
} 
} // end of client 

채팅 서버

// Chat Server runs at port no. 9020 
import java.io.*; 
import java.util.*; 
import java.net.*; 
import static java.lang.System.out; 

public class ChatServer { 
    ArrayList<String> users = new ArrayList<String>(); 
    ArrayList<String> buffer = new ArrayList<String>(); 
    ArrayList<HandleClient> clients = new ArrayList<HandleClient>(); 
    Socket client; 

    public void process() throws Exception { 
     ServerSocket server = new ServerSocket(9020,10); 
     out.println("Server Started..."); 
     while(true) { 
     client = server.accept(); 
     HandleClient c = new HandleClient(client); 
     clients.add(c); 
    } // end of while 
    } 
    public static void main(String ... args) throws Exception { 
     new ChatServer().process(); 
    } // end of main 

    public void bc(String user, String message) { 
     // send message to all connected users 
     for (HandleClient c : clients) 
      if (! c.getUserName().equals(user)) 
       c.sendMessage(user,message); 
    } 

    class HandleClient extends Thread { 
     String name = ""; 
    BufferedReader input; 
    PrintWriter output; 

    public HandleClient(Socket client) throws Exception { 
     // get input and output streams 
    start(); 
    input = new BufferedReader(new InputStreamReader(client.getInputStream())) ; 
    output = new PrintWriter (client.getOutputStream(),true); 
    output.println("Welcome to Kimberly's Chat room"); 
    // receives the name of the client 
    output.println("Enter your name: "); 
    // read name of the client 
    name = input.readLine(); 
    users.add(name); // adds the name of the client to the vector 
    output.println("Thanks for joining " + name + ". If you need help, type \"help\" for a list of commands."); 
    //start(); 
     } 

     public void sendMessage(String uname,String msg) { 
     output.println(uname + ":" + msg); 
    } 

     public String getUserName() { 
      return name; 
     } 

    public String toString() { 
      return name; 
     } 

     public void run() { 
      String line; 
     try { 
       while(true) { 
      line = input.readLine(); 
      String[] temp; 
      temp = line.split(":"); 
       //checks different input from the client 
      //checks to see if the client wants to terminate their connection 
      //removes the client's name from the list 
      if ("adios".equals(line)){ 
       output.println("Server closing connection..."); 
       clients.remove(this); 
       users.remove(name); 
       break; 
        } 
      else if("get".equals(line)){ 
       output.println(buffer.toString()); 
      } 
      else if("getNames".equals(line)){ 
       output.println(users.toString()); 
      } 
      //checks to see if the client typed in help to receive a list of the commands 
      else if("help".equals(line)){ 
       output.println("Here is a list of user commands:"); 
       output.println("adios: exit"); 
       output.println("get: receives a response of the entire chat buffer"); 
       output.println("help: lists the commands and their syntax"); 
       output.println("name: receives a response of \"OK\" and adds the name to a list"); 
      } 
      else if("name".equals(temp[0])){ 
       users.add(temp[1]); 
       output.println("OK"); 
      } 
      else if("push".equals(temp[0])){ 
       buffer.add(name + ":" + temp[1]); 
       output.println("OK"); 
      } 
      else if("test".equals(temp[0])){ 
       output.println(temp[1].toString()); 
      } 
      else{ 
       bc(name,line); // method of outer class - send messages to all 
      } 
      } // end of while 
     } // try 
     catch(Exception e) { 
      System.out.println(e.getMessage()); 
     } 
     try{ 
     client.close(); 
     } 
     catch(Exception e) { 
      System.out.println(e.getMessage()); 
     } 
     } // end of run() 
    } // end of inner class 

} // end of Server 

답변

0

NT 코드가 대부분 오류가 있습니다 : main() 시작, 연결, 서버에서 한 줄의 입력을 읽은 다음 서버에 내용을 쓰거나 사용자 입력을 요구하지 않고 종료합니다.

서버가 클라이언트를 더 정상적으로 처리해야합니다. 서버는 이해하기가 훨씬 더 어렵지만 클라이언트 입력을 받아들이는 모든 곳에서는 클라이언트가 연결이 끊어 졌음을 나타내는 예외를 처리 할 준비가되어 있어야합니다. 은 연결 해제를 위해으로 요청하십시오. (셀 재부팅이 죽었을 때 ... 예외가 발생하면 코드가 어떤 클라이언트가 담당하고 있는지 파악하고 연결된 목록에서 제거해야합니다.

관련 문제