2011-11-18 10 views
0

Javaスレッドに問題があります。このプログラムでは、TCPとUDPを同時に読み取るようにしたいのですが、コードではTCPリクエストが送信されたときにのみコードがUDPに進みます。Javaでスレッドを使用したソケットプログラミング

私は彼らに同時に働きたがっていますが、誰でも助けてくれますか?ここで

は、私がこれまで持っているものです。

public class Newthreads { 
    ServerSocket socket; 
    DatagramSocket udpSocket; 
    private int id=1; 

    public Newthreads() throws IOException { 
    socket=new ServerSocket(9000); 
    udpSocket=new DatagramSocket(5000); 
    System.out.println("listening on 7000"); 
    System.out.println("udp listening at 5000"); 
    ClientServerThread clientThread=new ClientServerThread(socket);`` 
    clientThread.start(); 
    SlientServerThread e =new SlientServerThread(udpSocket); 
    e.start(); 
    } 

    public static void main(String[] args) throws IOException { 
    new Newthreads(); 
    } 
} 

class ClientServerThread extends Thread { 
    Socket clientSocket; 
    int child; 
    public ClientServerThread(ServerSocket conn) throws IOException { 
    //To change body of created methods use File | Settings | File Templates. 
    System.out.println("i m here"); 
    clientSocket=conn.accept(); 
    } 
    public void run() { 
    System.out.println("executing TCP"); 
    } 
} 

class SlientServerThread extends Thread { 
    Socket conn; 
    DatagramPacket recvPacket; 
    private byte[] recvdata=new byte[10]; 

    SlientServerThread(DatagramSocket tcpSocket) throws IOException { 
    recvPacket=new DatagramPacket(recvdata,recvdata.length); 
    tcpSocket.receive(recvPacket); 
    System.out.println("hey thread 2"); 
} 
+0

これを見てください:http://codetoearn.blogspot.com/2013/01/multi-thread-tcp-socket-programming.html – ehsun7b

答えて

3

あなたはTCP接続がでてくるまでブロックされClientServerThreadコンストラクタで「受け入れる」をやっているあなたは、コンストラクタまでスレッドの開始になってされることはありません。完了する。

関連する問題