I keep getting this get this Exception:
java.io.StreamCorruptedException: invalid stream header: 00000001
Server side I used this to send and receive int, works fine.
Server:
new DataOutputStream(player1.getOutputStream()).writeInt(P1);
Client:
dataFromServer = new DataInputStream(socket.getInputStream());
dataFromServer.readInt();
But when I try to send an object, like this, it gives the error.
Server:
new ObjectOutputStream(player2.getOutputStream()).writeObject(gameCrossword);
Client:
objectFromServer = new ObjectInputStream(socket.getInputStream());
crossword = (Crossword)objectFromServer.readObject();
Any help would be good. Here is me sending the crossword initially prior to game session
I changed the code to use only object streams rather than data streams, upon the advice of jtahlborn
server
player1 = serverSocket.accept();
serverLog.append(new Date() + ": Player 1 joined session " + sessionNo + '\n');
serverLog.append("Player 1's IP address" + player1.getInetAddress().getHostAddress() + '\n');
new ObjectOutputStream(player1.getOutputStream()).writeInt(P1);
new ObjectOutputStream(player1.getOutputStream()).writeObject(gameCrossword);
player2 = serverSocket.accept();
serverLog.append(new Date() + ": Player 2 joined session " + sessionNo + '\n');
serverLog.append("Player 2's IP address" + player2.getInetAddress().getHostAddress() + '\n');
new ObjectOutputStream(player2.getOutputStream()).writeInt(P2);
new ObjectOutputStream(player2.getOutputStream()).writeObject(gameCrossword);
client
private void connectToServer() {
try {
Socket socket = new Socket(host, 8000);
objectFromServer = new ObjectInputStream(socket.getInputStream());
objectToServer = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ex) {
System.err.println(ex);
}
Thread thread = new Thread(this);
thread.start();
}
#Override
public void run() {
try {
player = objectFromServer.readInt();
crossword = (Crossword)objectFromServer.readObject();
System.out.println(crossword);
regards,
C.
don't wrap the socket streams with more than one input/output streams. this will break in all kinds of bad ways. in this specific case, the ObjectInputStream reads a header from the stream on construction, which is happening before you have read the int from the stream. regardless, just use a single ObjectOutputStream and ObjectInputStream and ditch the Data streams (note that ObjectOutputStream has a writeInt method).
Related
I'm having the following problem in java: I am developing and app using java.net.Socket. It looks like that: There is a server with a thread which accepts and adds new client, and another thread which reads data from sockets and does "something" with it. Next to it there are clients. Client has data reader thread as well as a separate thread. I send the data as simple as:
socket.getOutputStream().write((content+"\n").getBytes());
on the client side and read it on the server like:
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String received;
while(true) {
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
received = reader.readLine();
if(received == null) {
break;
}
System.out.println("SERVER " + received);
increaseReceivedCounter(1);
} catch(SocketException e) {
break;
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("SERVER RECEIVED "+ getReceivedCounter() + " MESSAGES!");
}
Now I just set the client to send some amount of messages like this:
try {
int n = 1000;
System.out.println("sending "+ n +" messages to " + client);
for(int i=0 ; i<n ; ++i) {
socket.getOutputStream().write((content+"\n").getBytes());
}
System.out.println("done sending " + n + " messages");
} catch (IOException e) {
e.printStackTrace();
}
The problem is that not all of the messages are transferred to a server. I have been looking for some solution for this but didn't manage to achieve 100% reliability. Is it even possible? I also tried with read instead of readLine but the result is the same: sometimes even 90% data loss. I think while server is working on the received data it ignores incoming packets and they're just lost.
Edit
Sockets initializations:
serverSocket = new ServerSocket(Server.PORT);//PORT = 9876, whatever
for the data reader on server side:
socket = serverSocket.accept();
on the client:
socket = new Socket("127.0.0.1", Server.PORT)
This is not an 'efficiency issue'. It is a bug in your code.
The problem is that not all of the messages are transferred to a server.
No, the problem is that you are losing data at the server. This is because you keep recreating BufferedReaders. You should create it once for the life of the socket.
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Remove this line.
The way you have it, you will lose data every time the prior BufferedReader has, err, buffered.
You also need to close the socket.
Im making an app that has to send a class to a server written in c++ using Sockets. The class consists of to variables both are Ints. I want to convert the java class into bytes then send it over the socket as a packet. The server is expecting 8 bytes for the packet size. When I try to convert my object I get more than 8 bytes. How else can I send my object to the server? Also the my code below sends 4 bytes of data in two 2 bytes chucks. Why is it doing that?
public void connect2() {
String serverHostname = new String("My IP");
ObjectOutputStream out2 = null;
ObjectInputStream in2 = null;
try {
echoSocket = new Socket(serverHostname, MYPORT);
StatusPacket p = new StatusPacket();
byte[] data = new byte[8];
data = serializeObject(p);
int j = data.length;
out2 = new ObjectOutputStream(echoSocket.getOutputStream());
out2.flush();
in2 = new ObjectInputStream(echoSocket.getInputStream());
DataOutputStream dOut = new DataOutputStream(echoSocket.getOutputStream());
out2.write(data);
out2.close();
in2.close();
echoSocket.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + serverHostname);
System.exit(1);
}
}
ObjectOutputStream and ObjectInputStream use Java serialization mechanisms, which includes a lot more info than just the class property values. You don't want to deal with those in C++ code, so I recommend you remove all serialization code and those two streams from your code.
Since you already know what you want on byte level, you should really be using that DataOutputStream instead. It allows you to transfer scalar data types like byte, int, long etc. without any overhead. Just get those two 32-bit integers from your object and pass them to DataOutputStream.writeInt(..) and you're set.
I wanted to send/receive continuous stream of data from one endpoint to another(peer2peer) with push and pull 'able asynchronously
So to first solve communication , I started with jax-ws soap binding webservice since it has an endpoint and ws-addressing for push mechanism but it seems to be a lot of overhead (heavy as per the docs and since unfamiliar with ws-*, I haven't implemented it , as I need multiple clients listening to the stream at a later point and the stream is 24/7 I wanted thread manageable sockets).
Then I took jax-rs but it does not include ws-addressing in it.(jax-rs 2.0)
I also looked at websockets but it required an app server but I want a jvm supportable code
So, Now I am trying to use basic sockets but the problem I am having is streaming the data through socket at server and client receiving it continuously.
It is working for the first read but no further.
Secondly, how can I make it asynchronous?
public class sSocket {
public static void main(String args[]) throws IOException{
int i = 15000;
ServerSocket ss;
Socket socket = null;
ss = new ServerSocket(i);
try
{
socket = ss.accept();
socket.setKeepAlive(true);
int iii = 0;
System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(iii<9)
{
Thread.sleep(2000);
output.write("good" + iii + "\n");
//System.out.print(input.readLine().toString());
output.flush();
iii++;
}
//socket.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public class cSocket {
public static void main(String args[]) throws InterruptedException, IOException{
Socket client = new Socket("127.0.0.1", 15000);
try{
client.setKeepAlive(true);
DataOutputStream out = new DataOutputStream(client.getOutputStream());
out.writeBytes("Hi Server! I'm " + client.getLocalSocketAddress() + "\n" );
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
String s;
while(true){
if((s = input.readLine()) != null)
{
System.out.println("Message from Server: " + s);
}}
//client.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
[toString() unavailable - no suspended threads] I see this halting the code in eclipse.
The problem seems to be essentially rooted in the input.readLine() in client: error is connection reset : which I assume is because readLine() has reached "EOF"
Don't keep creating new streams. Use the same ones for the life of the socket, at both ends. You're losing data in the buffers.
You don't need to keep calling setKeepalive(). Once is enough.
I have 2 classes (Client and Server) used to implement simple communication in my application. My code is shown below:
Server:
public class Server {
public static void main(String[] ar) {
int port = 1025; // just a random port. make sure you enter something between 1025 and 65535.
try {
ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number.
System.out.println("Waiting for a client...");
Socket socket = ss.accept();
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("enter meter id ");
String line = null;
while (true) {
line = in.readUTF(); // wait for the client to send a line of text.
System.out.println("client send me this id number " + line);
line = keyboard.readLine();
out.writeUTF(line);
out.flush();
//line = in.readUTF();
System.out.println("Waiting for the next line...");
System.out.println();
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
Client:
public class Client {
public static void main(String[] ar) {
int serverPort = 1025;
String address = "localhost";
try {
InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
System.out.println(" IP address " + address + " and port "
+ serverPort);
Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Create a stream to read from the keyboard.
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
System.out.println("ClientConnected.");
System.out.println("enter meter id");
while (true) {
line = keyboard.readLine(); // wait for the user to type in something and press enter.
System.out.println("Sending this number to the server...");
out.writeUTF(line); // send the above line to the server.
out.flush(); // flush the stream to ensure that the data reaches the other end.
line = in.readUTF(); // wait for the server to send a line of text.
System.out
.println("The server was very polite. It sent me this : "
+ line);
System.out.println();
}
}
catch (Exception x) {
x.printStackTrace();
}
}
}
My problem is that while testing the program I do get communication between the client and server, but while debugging, with a break point on the out.flush line in Server.java, it does not go to the intended destination. This intended destination being the line line = in.readUTF(); of Client.java. Can anyone help me to solve this?
It is good practice to open the OutputStreams before the InputStreams, on your sockets, as said in this question.
This question also clarifies that.
What I suspect here is your client and server are running in two different JVM processes and java debugger cannot debug two JVM at the same time.
I'm creating a very simple Java chat program, using the Java TCP sockets. I'm new to socket programming and Java. I cannot connect with server, because every time the client connects to server it times out. Maybe, it is because I'm typing the wrong IP address——I don't know.
Here is the code for the Server:
try
{
int fport = Integer.valueOf(port.getText());
ServerSocket server = new ServerSocket(fport);
Socket socket = server.accept();
msg.append("\\n Server is listening to port:" + port.getText());
BufferedReader input = new BufferedReader( new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print(msgtxt.getText());
msg.append("\n\n" + input.readLine());
msg.append("\n\n" + Nombre.getText() + msgtxt.getText());
}
catch (Exception ex)
{
msg.setText("\n\n" + "Error:" + ex.getMessage());
}
Here is the code for the Client:
try
{
int iport = Integer.valueOf(port.getText());
int i1;
int i2;
int i3;
int i4;
i1 = Integer.valueOf(ip.getText());
i2 = Integer.valueOf(ip1.getText());
i3 = Integer.valueOf(ip2.getText());
i4 = Integer.valueOf(ip3.getText());
byte[] b = new byte[] {(byte)i1, (byte)i2, (byte)i3, (byte)i4 };
InetAddress ipaddr = InetAddress.getByAddress(b);
Socket sock = new Socket(ipaddr, iport);
BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
output.write(m.getText());
while(!input.ready()){}
msg.setText("\n\n" + input.readLine());
msg.setText("\n\n" + m.getText());
output.close();
input.close();
}
catch (Exception ex)
{
msg.setText("\n\n" + "Error: " + ex.getMessage());
}
verify that you can connect to the server using telnet (on windows you may need to install it as it's not installed by default anymore).
basically, open a connection to your server and see that it works:
telnet host port
if it works, maybe the problem is not in establising the connection but in waiting for a response from the server (add the exception to your question).
one note:
you can open a socket without creating the INetAddress as you did, just new Socket(hostname, port).