Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初学者Java套接字编程错误_Java_Sockets_Client_Port - Fatal编程技术网

初学者Java套接字编程错误

初学者Java套接字编程错误,java,sockets,client,port,Java,Sockets,Client,Port,我开始编写我的第一个Java网络程序,长话短说,我很难确保我采取了正确的方法。我们的教授给了我们一个服务器程序来测试这个UDP客户端,但是我遇到了一些错误,我似乎无法克服。具体地说,我得到IO异常,“连接被拒绝”或“没有到主机的路由”异常 public class Lab2Client { /** * @param args[1] == server name, args[2] == server port, args[3] == myport */ public static void m

我开始编写我的第一个Java网络程序,长话短说,我很难确保我采取了正确的方法。我们的教授给了我们一个服务器程序来测试这个UDP客户端,但是我遇到了一些错误,我似乎无法克服。具体地说,我得到IO异常,“连接被拒绝”或“没有到主机的路由”异常

public class Lab2Client {
/**
 * @param args[1] == server name, args[2] == server port, args[3] == myport
 */
public static void main(String[] args) {
    //Serverport is set to 10085, our client is 10086
    try {
        Socket echoSocket = new Socket(args[0],Integer.parseInt(args[2]));
        System.out.println("Server connection Completed\n");
        DataOutputStream output = new DataOutputStream(echoSocket.getOutputStream());
        byte[] toSend = new byte[5];
        toSend[0] = 12; toSend[1] = 34;//Code Number
        toSend[2] = 15;//GroupId
        toSend[3] = 86;toSend[4] = 100;//Port number in Little Endian Order
        output.write(toSend);

        System.out.println("Sent Request. Waiting for reply...\n");

        DataInputStream input = new DataInputStream(echoSocket.getInputStream());

        byte[] toRecieve = new byte[]{0,0,0,0,0,0,0,0}; 
        input.read(toRecieve);
        checkMessage(toRecieve);            
    }
    catch (UnknownHostException e) {
        System.err.println("Servername Incorrect!");
        System.exit(1);
    }
    catch (IOException e){
        System.err.println("IO Exception. Exiting...");
        System.err.println(e);
        System.exit(1);
    }
}
关于在Java中接收消息的实现,我还有一些问题。我将得到一个数据报,其中包含:

a)3个格式化字节(对问题不重要)以及IP和端口号

b)3个格式化字节和一个端口


使用DataInputStream是正确的方法吗?我知道使用一个包含9个元素的数组是懒惰的,而不是动态地分配一个5或9元素的数组,但是现在我只是想让它工作起来。也就是说,有没有其他人会对此提出不同的建议?

您不需要将
Socket.getOutputStream()
返回的流包装为
DataOutputStream
——它已经是
DataOutputStream

在这方面:

Socket echoSocket = new Socket(args[0],Integer.parseInt(args[2]));
我想应该是args[1],而不是args[0]

在这里,您必须将整数值转换为其字节表示形式:

   toSend[3] = 10086 & 0xFF;toSend[4] = 10086>>8; //Port number in Little Endian Order

回答您的问题:caseb由于您没有发送
IP

我想我会把这个留给后代。问题很简单,我是个傻瓜,没有早点注意到

我测试的正确程序使用了UDP协议,这个程序是用TCP编写的。更正后的代码为:

public class Lab2Client {
/**
 * @param args[0] == server name, args[1] == server port, args[2] == myport
 */
public static void main(String[] args) {
    //Serverport is 10085, our client is 10086
    try {
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName(args[0]);
        int portToSend = Integer.parseInt(args[2]);
        System.out.println("Clent Socket Created");
        byte[] toSend = new byte[5];
        toSend[0] = 0x12; toSend[1] = 0x34;//Code Number
        toSend[2] = 15;//GroupId, f in hex
        toSend[3] = 0x27;toSend[4] = 0x66;
        System.out.println("Byte Array Constructed");

        DatagramPacket sendPacket = new DatagramPacket(toSend, toSend.length, IPAddress, Integer.parseInt(args[1]));
        clientSocket.send(sendPacket);          
        System.out.println("Sent Request. Waiting for reply...\n");

        DataInputStream input = new DataInputStream(echoSocket.getInputStream());
        toRecieve can either be an error message, a return of what we sent,
        or a byte stream full of IP info and port numbers.
        the "heavy" byte stream is either 4 for IPv4 of 16 for IPv6, 2 bytes for port,
        and the magic number (2 bytes) for a total of 9-20 bytes*/

        byte[] toRecieve = new byte[9];
        DatagramPacket receivePacket = new DatagramPacket(toRecieve, toRecieve.length);
        clientSocket.receive(receivePacket);            
        checkMessage(toRecieve);            
    } //and so on and so forth...

感谢@Serge的帮助,尽管没有人能正确回答我的问题,我是如何问的。您建议的字节移位也很重要。

听起来更像是硬件/路由或防火墙问题,但我花了几分钟研究了代码:PI也会考虑这一点,但我的教授提供的用Java编写的测试程序也使用了我提供给该程序的计算机名和端口。我也有助教,但显然他根本不被允许运行我的代码——他也只被允许目视检查它>:|