Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

java.net.SocketException: Software caused connection abort: socket write er

843790Dec 20 2006 — edited Dec 22 2006
Hi. I am writing a multiplayer chekers game. But i have a problem with networking. I got this error:java.net.SocketException: Software caused connection abort: socket write error
	try {
			s = new Socket(this.getCodeBase().getHost(), DEFAULT_PORT);
			in = new DataInputStream(s.getInputStream());
			out = new DataOutputStream(s.getOutputStream());
                        listener = new StreamListener(in, button_array);

			// Let the user know we are ready
			this.showStatus("Connected to "
					+ s.getInetAddress().getHostName()
					+ ":" + s.getPort());
		}
		catch (IOException e) { this.showStatus(e.toString()); }
and the other part of the code is...
try{
                        out.writeInt(c);
                        d=in.readInt();
                        System.out.println("-----"+d);
                    }
                    catch (IOException e2) {System.out.println(e2.toString()); };
Where am i wrong??

Comments

EJP
Nothing wrong here. What does the server code look like?
843790
import java.io.*;
import java.net.*;

// This server application opens a port and listens for clients to connect.
public class Server extends Thread {
	public final static int DEFAULT_PORT = 1701;
	protected int port;
	protected ServerSocket listen_socket;

	// Exit with an error message, when an exception occurs.
	public static void fail(Exception e, String msg) {
		System.err.println(msg + ": " + e);
		System.exit(1);
	}

	// Create a ServerSocket to listen for connections on; start the thread.
	public Server(int port) {
		if (port == 0) port = DEFAULT_PORT;
		this.port = port;
		try { listen_socket = new ServerSocket(port); }
		catch (IOException e) { fail(e, "Exception creating server socket"); }
		System.out.println("Server: listening on port " + port);
		this.start();
	}

	// The body of the server thread.  Loop forever, listening for and
	// accepting connections from clients.  For each connection pair,
	// create a Connection object to handle communication between clients
	// through the new Socket.
	public void run() {
		try {
			while(true) {
				// There must be two player to play a game.
				Socket client_socket1 = listen_socket.accept();
				Socket client_socket2 = listen_socket.accept();
				Connection c = new Connection(client_socket1,client_socket2);
			}
		}
		catch (IOException e) { fail(e, "Exception while listening for connections"); }
	}

	// Start the server up, listening on an optionally specified port
	public static void main(String[] args) {
		int port = 0;
		if (args.length == 1) {
			try { port = Integer.parseInt(args[0]); }
			catch (NumberFormatException e) { port = 0; }
		}
		new Server(port);
	}
}
And one more class is Connection:
import java.io.*;
import java.net.*;

// This is the class that handles all communication between two clients
class Connection extends Thread {
	protected Socket client1;
	protected Socket client2;
	protected DataInputStream in1;
	protected DataOutputStream out1;
	protected DataInputStream in2;
	protected DataOutputStream out2;

	// Initialize the streams and start the thread
	public Connection(Socket client_socket1,Socket client_socket2) {
		client1 = client_socket1;
		client2 = client_socket2;
		try {
			in1  = new DataInputStream(client1.getInputStream());
			out1 = new DataOutputStream(client1.getOutputStream());
			in2  = new DataInputStream(client2.getInputStream());
			out2 = new DataOutputStream(client2.getOutputStream());

			// Show that we have a connection.
			System.out.println("Connection Established.");
		}
		catch (IOException e) {
			try { client1.close(); } catch (IOException e2) {};
			try { client2.close(); } catch (IOException e2) {};
			System.err.println("Exception while getting streams: " +e);
			return;
		}
		this.start();
	}

	// Provide the service.
	// Because we don't trust the players to be honest, the server keeps
	// track of who goes where.  This follows the latest trend toward
	// large network servers, and dumb clients.
	public void run() {
		int box,turns,waste,i;
		int board[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		StringBuffer revline;
		int len;
		try {
			for(turns=0;turns<9;turns++) {
				// read in an int
				// decide who's turn it is
				// In this section we throw out an button
				// selections that the user tried to make
				// while it was not that user's turn.
				if (turns % 2 == 0) {
					waste = in1.available();

					// This is necessary because the
					// skipByte() method is hoplessly
					// borken.
					for(i=0;i<waste;i++) in1.readByte();

					box = in1.readInt();
				} else {
					waste = in2.available();

					// This is necessary because the
					// skipByte() method is hoplessly
					// borken.
					for(i=0;i<waste;i++) in2.readByte();

					box = in2.readInt();
				}
				// The clients send us the button number that
				// the user selected.  The server sets the
				// value at that location to the user's id. 
				// If the user tries to take an already taken
				// spot send a Nop.
				if (board[box] == 0) {
					// ok to go here
					i = (turns % 2);
					board[box] = i+1;
					out1.writeInt(box + (10*i));
					out2.writeInt(box + (10*i++));
			// check if there is a winner
			// I can't think of a pretter way to do this...

			// Test horazontal wins:
			if ((board[0] == i && board[1] == i && board[2] == i)||
			    (board[3] == i && board[4] == i && board[5] == i)||
			    (board[6] == i && board[7] == i && board[8] == i)||

			// Test vertical wins:
			    (board[0] == i && board[3] == i && board[6] == i)||
			    (board[1] == i && board[4] == i && board[7] == i)||
			    (board[2] == i && board[5] == i && board[8] == i)||

			// Test diagnal wins:
			    (board[0] == i && board[4] == i && board[8] == i)||
			    (board[2] == i && board[4] == i && board[6] == i)) {

				// The last player to move wins.
				if (turns % 2 == 0) {
					out1.writeInt(255); // Winner code.
					out2.writeInt(254); // Loser code.
				} else {
					out1.writeInt(254); // Loser code.
					out2.writeInt(255); // Winner code.
				}
				break;
			    }
				} else {
					// already occupied
					out1.writeInt(9);
					out2.writeInt(9);
					turns--;  // don't count that choice.
				}
			}
		}
		catch (IOException e) {}
		finally {
			try {
				client1.close();
				client2.close();
			} catch (IOException e2) {};
		}
		// Indicate that the game has ended.
		System.out.println("Disconnect.");
	}
}
I don't need the buttons part,because its from a tictactoe game, i will modify this for checkers.
EJP
Well you're writing out two ints on every turn from the server and only reading one at the client.

What's all that 'waste' stuff about? And what's hopelessly broken about InputStream.skipBytes()? All this looks like the wrong solution to a bug to me. Why are you skipping input at all?
843790
Ok. I changed my code. Here is the code..
 try {
                    for(i=0;;i++){
                            a=in1.readLine();
                            c=in2.readLine();
                            if(a.length()>0)
                            {
                                findMoves(a);  
                            }
                            else if(c.length()>0){
                                findMoves(c);
                            }
                            b=r1+":"+ c1+":"+r2+":"+c2;
                            out1.println(b);
                            out2.println(b);
                                                      
                   
                           
                    }
  }
There are two clients with the bufferedreaders in1,in2. The thing that i want to do is,when first client makes a move, this code gets it and print out to both clients as a string like row1+""+c1...

But now when i make the action from one client, nothng happens,after a while a lot of pieces move place.

I think that sth wrong in that code.

PS: I want to use it for a checkers game so in these code i have to write sth when its 1st clients turn,only he can move,otherwise gets a message.
1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 19 2007
Added on Dec 20 2006
4 comments
259 views