2011-11-18 3 views
0

자바 스레드에 문제가 있습니다. 이 프로그램에서는 TCP와 UDP를 동시에 읽으려고하지만 코드에서 TCP 요청이 전송 된 경우에만 코드가 UDP로 진행됩니다.Java에서 스레드를 사용하는 소켓 프로그래밍

나는 그들이 동시에 일하기를 원한다. 누가 도와 줄 수 있니? . 당신은 TCP 연결 당신은 결코 생성자 때까지 스레드 시작 받고되지 않습니다에 제공 될 때까지 차단됩니다 ClientServerThread 생성자에서 "동의 함"을하고있는

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

을 : 여기

내가 지금까지 가지고 무엇을 완료됩니다.