繁体   English   中英

初学者Java套接字编程错误

[英]Beginner Java Socket Programming Errors

我开始编写我的第一个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的数组,但是现在我只是想让它工作。 话虽这么说,有没有人会为此建议一种不同的方法?

您不需要使用DataOutputStream包装Socket.getOuputStream()返回的流 - 它已经是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

回答你的问题:案例b,因为你没有发送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的帮助,虽然没人能按我的要求正确回答我的问题。 你建议的字节转移也很重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM