server-clients java

时间:2013-04-11 00:20:30

标签: java client-server

我想让我的服务器能够接受来自两个客户端的消息。但出了点问题。需要帮助!我尝试两次运行相同的客户端,但只收到一条消息和一些错误。

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

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

        Listner listner = new Listner();
        Thread thread = new Thread(listner);
        thread.start();

        System.out.println("thread already started");

        DatagramSocket s = new DatagramSocket(8787);

        byte[] receiveData = new byte[1024];

        System.out.println ("Waiting for datagram packet");

        DatagramPacket p = new DatagramPacket(receiveData, receiveData.length); 

        System.out.println("about to receive packet N1");
        s.receive(p);

        String sentence = new String(p.getData()); 
        InetAddress IPAddress = p.getAddress(); 
        int port = p.getPort(); 

        System.out.println ("From: " + IPAddress + ":" + port);
        System.out.println ("Message: " + sentence);



        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }

        receiveData = new byte[1024];

        System.out.println ("Waiting for datagram packet");
        p =  new DatagramPacket(receiveData, receiveData.length); 

        System.out.println("about to receive packet N2");

        s.receive(p);
        sentence = new String(p.getData()); 
        IPAddress = p.getAddress(); 
        port = p.getPort(); 
}

}


class Listner implements Runnable
    { 

        public void run() {
        String text = null;
        while(true){
            text = null;    
        int server_port = 8787;

         byte[] receiveData = new byte[1024];
        DatagramPacket p = new DatagramPacket(receiveData, receiveData.length); 
        DatagramSocket s = null;
        try{
           s = new DatagramSocket(server_port);
        }catch (SocketException e) {
            e.printStackTrace();
            System.out.println("Socket excep, port used");
        }
        try {
        s.receive(p);
       }catch (IOException e) {
            e.printStackTrace();
                System.out.println("IO EXcept");
            }
        text = new String(receiveData, 0, p.getLength());
        System.out.println("message = "+text);
        s.close();

    }
}

}

运行:

  

线程已经开始

     

等待接收数据包N1的数据报包

java.net.BindException: Address already in use: Cannot bind
  at java.net.PlainDatagramSocketImpl.bind0(Native Method)
  at java.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:91)
  at java.net.DatagramSocket.bind(Socket excep, port used
DatagramSocket.java:372)
  at java.net.DatagramSocket.<init>(DatagramSocket.java:211)
  at java.net.DatagramSocket.<init>(DatagramSocket.java:262)
  at java.net.DatagramSocket.<init>(DatagramSocket.java:235)
  at serv1.Listner.run(Listner.java:29)
  at java.lang.Thread.run(Thread.java:662)
Exception in thread "Thread-0" java.lang.NullPointerException
  at serv1.Listner.run(Listner.java:35)
  at java.lang.Thread.run(Thread.java:662)
From: /192.168.1.102:58977
Message: hi, i am the first one
Waiting for datagram packet
about to receive packet N2
BUILD SUCCESSFUL (total time: 59 seconds)**strong text**

2 个答案:

答案 0 :(得分:0)

您正尝试将同一端口绑定两次。这不是它的工作原理。您应该有1个线程侦听端口,以及一个线程池来处理请求。每当新信息被认为是端口时,您将请求数据(收到的新信息)提供给线程进行处理。

答案 1 :(得分:0)

您正在同一端口(8787)上创建2个DatagramSocket。一个是在main方法中创建的,另一个是在Listner内部类中创建的。 run()方法。

我不确定您在这里尝试做什么,但您需要运行Listner课程&#39; DatagramSocket位于不同的端口上,或者将main()方法创建的方法传递给Listner类。