2014-04-20 2 views
0

친구들이이 클라이언트와 서버 채팅 프로그램에 질려합니다. 내 프로그램이 컴파일되고 실행 중이지만 문제가 서버에 메시지를 전달하려고 할 때 작동하지 않는 것입니다.자바 클라이언트 서버 채팅 프로그램

import java.io.*; 
import java.net.*; 

class serv 
{ 
    ServerSocket s; 
    Socket c; 
    DataInputStream dis; 
    DataOutputStream dos; 
    BufferedReader disi; 

    public serv() 
    { 
      try 
      { 
       s = new ServerSocket(2000,0,InetAddress.getLocalHost()); 
       System.out.println("Server is Created"); 
       c = s.accept(); 
       System.out.println("Request Accepted"); 
      } 
      catch(Exception e) 
      { 
       System.out.println(e); 
      } 
    } 

    public void talk()throws IOException,UnknownHostException 
    { 
      dis = new DataInputStream(c.getInputStream()); 
      dos = new DataOutputStream(c.getOutputStream()); 
      disi = new BufferedReader(new InputStreamReader(System.in)); 

      while(true) 
      { 
       String str = new String(disi.readLine()); 
       dos.writeUTF(str); 
       System.out.println(str); 
      } 
    } 

    public static void main(String args[]) 
    { 
      try 
      { 
       serv c = new serv(); 
       c.talk(); 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
    } 
} 

클라이언트 코드 :

import java.io.*; 
import java.net.*; 

class clien 
{ 
    Socket c; 
    DataInputStream dis; 
    BufferedReader disi; 
    DataOutputStream dos; 

    public clien()throws IOException,UnknownHostException 
    { 
      c=new Socket(InetAddress.getLocalHost(),2000); 
      System.out.println("Request is sended"); 
    } 

    public void talk()throws IOException,UnknownHostException 
    { 
      try 
      { 
       dis=new DataInputStream(c.getInputStream()); 
       dos=new DataOutputStream(c.getOutputStream()); 
       disi=new BufferedReader(new InputStreamReader(System.in)); 

       while(true) 
       { 
        String str=new String(disi.readLine()); 
        dos.writeUTF(str); 
        System.out.println(str); 
       } 
     } 
     catch(Exception e) 
     { 
       e.printStackTrace(); 
     } 
    } 

    public static void main(String args[]) 
    { 
      try 
      { 
       clien c=new clien(); 
       c.talk(); 
      } 
      catch(Exception e){ } 
    } 
} 

답변

0

문제의 톤이있다

서버 코드 ... 내가 무엇을 보정 ..now. 이 같은 프로토콜의 일종 수행하려는 것처럼

것 같다 : 클라이언트가 서버로 메시지를 전송

클라이언트가 서버에 연결을 서버는 메시지를

피어 - 투 - 피어 유형을 수신 체계. 서버를 다른 클라이언트 (클라이언트에 메시지를 입력하기 위해 메시지를 입력 함)로 볼 수 있을지 확신하지는 않지만 연결이 설정되면 바로 클라이언트와 서버가 루프로 연결됩니다. 이 루프에는 집중할 수있는 것은 단 하나뿐입니다.

클라이언트 : 메인 (문자열 []) -> 연결 -> 사용자 (루프)에서 입력 시작 프로그램을 읽기 -> 연결 -> 서버에서 정보를 원하시면 듣기 시작

서버 : 메인 (문자열 [ ]) -> 연결 허용 -> 사용자 입력 읽기 (루프)

클라이언트가 서버에서 정보를 수신하고 정보를 보낼 수있게하려면 2 개의 스레드가 필요합니다.

static Socket s; 
static DataOutputStream out; 
static DataInputStream in; 

public static void main(String[] args) { 
     try { 
      s = new Socket("host", 2000); 
      out = new DataOutputStream(s.getOutputStream()); 
      in = new DataInputStream(s.getInputStream()); 

      new Thread(new Runnable() { 
       public void run() { 
        Scanner scanner = new Scanner(System.in); 

        String input; 
        while(!(input = scanner.nextLine()).equals("EXITPROGRAM")) { 
          out.writeUTF(input); //sends to client 
        } 
       } 
      }).start(); 

      while(true) { 
       String infoFromServer = in.readUTF(); 

       //you can print to console if you want 
      } 
     }catch(Exception e) { } 
} 

이제 클라이언트는 사용자 (콘솔에서)의 입력을 받아 서버에서 데이터를 수신 할 수 있습니다. 서버에 대해 동일한 구조를 사용할 수도 있습니다.