简体   繁体   中英

Sending packets over TCP socket

I'm writing this tiny utility method to test sending raw packets to a specific messaging network (planning on developing a client to connect to it).

The network is the Deviantart messaging network (chat.deviantart.com:3900; TCP).

My class:

protected void connect() throws IOException{

    Socket dAmn = null;
    //BufferedWriter out = null;
    PrintWriter out = null;
    BufferedReader in = null;

    /*
     * Create Socket Connection
     */
    try{
        dAmn = 
            new Socket("chat.deviantart.com", 3900);
        /*out =
            new BufferedWriter(new OutputStreamWriter(dAmn.getOutputStream()));*/
        out =
            new PrintWriter(dAmn.getOutputStream(), true);
        in =
            new BufferedReader(new InputStreamReader(dAmn.getInputStream()));
    }
    catch(SocketException e){
        System.err.println("No host or port for given connection");
        //handle
    }
    catch(IOException e){
        System.err.println("I/O Error on host");
        //handle
    }
    String userInput;
    BufferedReader userIn = 
                        new BufferedReader(new InputStreamReader(System.in));

    /*
     * dAmn communication
     */

    while((userInput = userIn.readLine()) != null){
        out.write(userInput);
        System.out.println(in.readLine());
    }
    if(in!=null)
        in.close();
    if(out!=null)
        out.close();
    if(dAmn!=null)
        dAmn.close();
}

The server requires a handshake to be sent before the login may proceed. A typical login packet looks like thus:

dAmnclient damnClient (currently 0.3) agent= agent

Every packet must end with a newline and a null.

My handshake packet would look something like:

dAmnClient 0.3\\nagent=SomeAgent\\n\\0

However the server simply replies with disconnect

I think something is incorrectly being parsed, any advice? Also, if you're super intersted in helping me out: here's some quick documentation on the client -> server dAmn protocol: http://botdom.com/wiki/DAmn#dAmnClient_.28handshake.29

You should use Wireshark

With Wireshark you can sniff traffic from/to hosts. It makes it really easy to spot where your application does something else than the standard client.

BTW you have a \\n in front of agent=, it might be the problem

The line read from the user will not contain the actual line termination, and it will not contain any null-termination either. Typing \\n at the input will actually transmit "\\n" rather than a new-line.

You could add a new-line by replacing write with println (careful, it may use \\n, \\r\\n or just \\r depending on platform):

out.println(userInput);

You could support packet termination eg by checking for a specific user input, like so:

if (userInput.equals(".")) {
  out.write((char) 0);
  out.flush();
} else {
  out.println(userInput);
}

The user can now terminate packets by typing a dot.

(Actually the code could perform the handshake automatically without waiting for user input, but that's another story.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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