Question

I'm just trying to test sending bytes over a TCP socket connection, I know it wasn't really meant for that but I'm just trying to figure out whether this is possible or not

what i'm trying to do:

  • get bytes from a string on client

  • sent it as bytes to the server

  • get the bytes on the server and decode it back to the original string

Client:

package ByteClientServer;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;

public class Client {


    String hostAddress = "localhost";
    int port = 1010;


    public Client()
    {
        try {

            Socket socket = new Socket(hostAddress, port);

            String test = "hello"; //dycrypt bytes from this string on server side

            byte[] byteArray = test.getBytes();

            OutputStream out = socket.getOutputStream(); 
            DataOutputStream dos = new DataOutputStream(out);

            dos.write(byteArray);

        } 
        catch (UnknownHostException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }


    public static void main(String[] args)
    {
        new Client();
    }




}

Server:

package ByteClientServer;

import java.io.*;
import java.net.*;


public class Server {

    public static void main(String[] args) 
    {
        try
        {                       
            ServerSocket server = new ServerSocket(1010);
            server.setSoTimeout(0);

            Socket connectionToClient = server.accept();
            InputStream is = connectionToClient.getInputStream();
            DataInputStream dis = new DataInputStream(is);


            byte[] data = dis.readUTF().getBytes();

            //dis.readFully(data, 0, data.length);

            String s = new String(data);

            System.out.println(s);


        }
        catch(IOException e)
        {
            e.printStackTrace();
            //System.err.println("Server was terminated.");
        }
    }
}

it doesn't like this line on server:

byte[] data = dis.readUTF().getBytes();

and throws the exception:

java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.io.DataInputStream.readFully(Unknown Source) at java.io.DataInputStream.readUTF(Unknown Source) at java.io.DataInputStream.readUTF(Unknown Source) at ByteClientServer.Server.main(Server.java:21)

Was it helpful?

Solution 3

I came up with a simple workaround

Client:

import java.io.*;
import java.net.*;

public class Client {


    public static void main(String[] args)
    {

        String hostAddress = "localhost";
        int port = 8080;

        Socket socket = null;


        String test = "hello"; //decode bytes from this string on the server

        byte[] byteArray = test.getBytes();


        try 
        {
            socket = new Socket(hostAddress, port);
            OutputStream out = socket.getOutputStream();
            DataOutputStream dos = new DataOutputStream(out);

            dos.write(byteArray, 0, byteArray.length);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
    }
}

Server:

import java.io.*;
import java.net.*;


public class Server {

    public static void main(String[] args) throws SocketException 
    {
        try
        {                       
            ServerSocket server = new ServerSocket(8080);
            server.setSoTimeout(0);

            Socket connectionToClient = server.accept();
            InputStream is = connectionToClient.getInputStream();
            DataInputStream dis = new DataInputStream(is);

            int buffersize = connectionToClient.getReceiveBufferSize();

            byte[] bytes = new byte[buffersize];


            if(dis.read(bytes) > 0)
            {
                String s = new String(bytes);
                System.out.print(s);
            }   


            dis.close();
            server.close();
        }
        catch(IOException e)
        {       
            e.printStackTrace();
            System.err.println("Server was terminated.");
        }
    }
}

OTHER TIPS

If you want to use readUTF then you need to use writeUTF. if you want to just write bytes, then you need to read just bytes.

You are writing bytes with default encoding then reading it as UTF-8 encoding. Thats the issue. Here is John Skeets blog explaining how to debug these errors and some pitfalls

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top