Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

socket reset ?

 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,i have a socket between 2 programs ,one program is sending
5 objects using ObjectInputStream ,and the other is in an endless loop
waiting for objects with the ObjectInputStream
when the 5 objects are sent an exception : socket reset stops the
program ,why is this exception occurs ?
here is the code:

import java.io.* ;
import java.net.* ;
public class receive {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1" , 3000) ;
ObjectInputStream in = new ObjectInputStream(s.getInputStream()) ;
while (true) {
String a = (String)in.readObject() ;
System.out.println("OK " + a ) ;
Thread.sleep(400) ;
}
}
catch (Exception exc) {
System.out.println("Error send "+exc.getMessage()) ;
}
}
}


class send {
public static void main(String[] args) {

try {
ServerSocket ss = new ServerSocket (3000) ;
Socket s = ss.accept() ;

ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()) ;
//out.flush() ;

for (int i = 0 ; i < 3 ; i++)
out.writeObject("Item :" + i) ;
Thread.sleep(400) ;
}
catch (Exception exc) {
System.out.println("Error send "+exc.getMessage()) ;
}
}
}

class Block implements Serializable{
boolean clicked = false;
String pieceColor ;
String pieceSens ;

Block(String c , String sens) {
clicked = false ;
pieceColor = c ; // pieceColor peut etre Red White nothing
pieceSens = sens ; // sens peut etre up down nothing
}


}
 
Catalin Merfu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your server program (send) accepts a client connections, sends 3 String objects to the client and then terminates. The client connection is automatically closed which causes the exception on the client side.

Remove the sleep(400) call in 'receive' and you'll read all the strings.
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx for the help, i'll try it today
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic