2011-01-18 9 views
-1

クライアントコードJava Eclipseでソケットプログラミングを使用してサーバークライアントコードを実行する方法は?

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

public class SimpleClient { 

    public static void main(String[] args) throws IOException { 

Socket clientSocket = null; 

try { 
    clientSocket = new Socket(args[0], 4442); 
} catch (UnknownHostException e) { 
    System.err.println("Don't know about host: " + args[0] + "."); 
    System.exit(1); 
} catch (IOException e) { 
    System.err.println("Couldn't get I/O for " + 
      "the connection to: " + args[0] + ""); 
    System.exit(1); 
} 

BufferedInputStream in; 
BufferedOutputStream out; 

try { 
    in = new BufferedInputStream(clientSocket.getInputStream()); 
    out = new BufferedOutputStream(clientSocket.getOutputStream()); 
} catch (IOException e) { 
    System.out.println(e.toString()); 
    return; 
} 

byte[] m_txt = args[1].getBytes(); 

out.write(m_txt, 0, m_txt.length); 

out.flush(); 

byte[] m_rcv = new byte[m_txt.length]; 

int n = in.read(m_rcv, 0, m_rcv.length); 

if (n != m_rcv.length) { 
    System.out.println("Some data are lost ..."); 
} 

System.out.println(new String(m_rcv)); 

out.close(); 
in.close(); 
clientSocket.close(); 
    } 
} 

サーバー

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

public class SimpleServer { 

    public static void main(String[] args) throws IOException { 

boolean listening = true; 
ServerSocket serverSocket = null; 
try { 
    serverSocket = new ServerSocket(4442); 
} catch (IOException e) { 
    System.err.println("Could not listen on port: 4444."); 
    System.exit(1); 
} 

while(listening) { 
    Socket clientSocket = serverSocket.accept(); 
    (new SimpleConHandler(clientSocket)).start(); 
} 

serverSocket.close(); 
    } 
} 

接続ハンドラ:

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

public class SimpleConHandler extends Thread 
{ 

    private Socket clientSocket; 

    public SimpleConHandler(Socket clientSocket) { 

this.clientSocket = clientSocket; 
    } 

    public void run() { 

BufferedInputStream in; 
BufferedOutputStream out; 

try { 

    in = new BufferedInputStream(clientSocket.getInputStream()); 
    out = new BufferedOutputStream(clientSocket.getOutputStream()); 

} catch (IOException e) { 
    System.out.println(e.toString()); 
    return; 
} 

try { 
    byte[] msg = new byte[4096]; 
    int bytesRead = 0; 
    int n; 

    while((n = in.read(msg, bytesRead, 256)) != -1) { 
    bytesRead += n; 
    if (bytesRead == 4096) { 
     break; 
    } 
    if (in.available() == 0) { 
     break; 
    } 
    } 

    for(int i=bytesRead; i>0; i--) { 
    out.write(msg[i-1]); 
    } 

    out.flush(); 

} catch(IOException e1) { 
    System.out.println(e1.toString()); 
} 

try { 
    out.close(); 
    in.close(); 
    clientSocket.close(); 
} catch (IOException e2) {;} 
    } 
} 

まず、私はサーバを実行するが、私はクライアントを実行しようとすると、私はgettiですエラー両方とも0 SimpleClient.mainで (SimpleClient.java:11)​​

私は実行するために別のコンソールを使用する必要がすることがある:スレッド "メイン" java.lang.ArrayIndexOutOfBoundsExceptionで

例外:NGですサーバーとクライアント?もしそうなら、私に方法を教えてください。私はJava Eclipse 1.6 SEを使用しています。

+1

次の時間は、それをフォーマットしてください。人々が読めるようにします。 – Raunak

答えて

1

clientSocket = new Socket(args[0], 4442);

あなたのプログラムは、コマンドライン引数を必要とする:あなたがソースコードを投稿

java your.Program <ip>

+1

つまり、例外がスローされた行を見てください。ほとんどのIDEでは、例外行をクリックするだけでそこに移動します。 –

関連する問題