Keep my TCP connection alive
Hi again,
I'm having trouble keeping my TCP connection between my server and client alive.
Please take a look at my code and tell me what I am doing wrong:
TCP CLIENT
Code:
package networkTCP;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.*;
import java.util.ArrayList;
public class tcpclient implements Serializable
{
public Socket echoSocket = null;
public ArrayList board = new ArrayList();
public void setupConnection() throws Exception
{
// keep connection alive here
echoSocket = new Socket("127.0.0.1", 2323);
}
public ArrayList tcpsend(String coords) throws Exception
{
//Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
//start socket, and send console text input
System.out.println("pressed");
try
{
//temp
//echoSocket = new Socket("127.0.0.1", 2323);
out = new PrintWriter(echoSocket.getOutputStream(), true);
//send coords to server
out.println(coords);
System.out.println("sent coords: " + coords);
//InputStreamReader strmRead = new
//InputStreamReader(echoSocket.getInputStream());
//in = new BufferedReader(strmRead);
// wait for reply from server
System.out.println("Still alive?");
//Socket serialsocket = new Socket("127.0.0.1", 2323);
System.out.println("Still alive?");
InputStream instr = echoSocket.getInputStream();
System.out.println("waiting for board object to be returned!");
ObjectInputStream ois = new ObjectInputStream(instr);
System.out.println("board object returned");
board = (ArrayList) ois.readObject();
ois.close();
//out.close();
//in.close();
//serialsocket.close();
//echoSocket.close();
//Thread.sleep(1000);
/*
int x = 0;
while (x != board.size()) {
System.out.println(board.get(x));
x++;
}
x=0;
*/
} catch (UnknownHostException e) {
System.err.println("Don't know about host: 127.0.0.1.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: 127.0.0.1.");
System.exit(1);
}
return board;
}
public static void main (String args[]) throws Exception
{
tcpclient client = new tcpclient();
client.setupConnection();
client.tcpsend("0,1");
client.tcpsend("1,0");
}
}
TCP SERVER
Code:
package networkTCP;
import java.net.*;
import java.io.*;
import java.io.Serializable;
public class tcpserver implements Serializable
{
// create gameboard object
public static gameboard.board board = new gameboard.board();
public static void serverstart() throws IOException,
InterruptedException
{
//what is setTCPNoDelay(); ??
System.out.println("Hi, I'm the TCP server!");
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(2323);
} catch (IOException e) {
System.err.println("listen error on port: 2323.");
System.exit(1);
}
while (listening)
{
new tcpserverthread(serverSocket.accept()).run();
}
//serverSocket.close();
}
public static void main(String[] args) throws IOException,
InterruptedException {
serverstart();
}
}
TCP SERVER-THREAD
Code:
package networkTCP;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
public class tcpserverthread extends tcpserver
{
//global socket
private Socket clientSocket = null;
public tcpserverthread(Socket socket)
{
//super("tcpserverthread");
this.clientSocket = socket;
}
public void run()
{
try {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream
(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine = new String();
System.out.println(clientSocket.getPort());
OutputStream oStream = clientSocket.
getOutputStream();
ObjectOutputStream ooStream = new
ObjectOutputStream (oStream);
inputLine = in.readLine();
while(true)
{
System.out.println("in1" + inputLine);
String[] splitInput = inputLine.split(",");
String colStr = new String (splitInput[0]);
String rowStr = new String (splitInput[1]);
int row = new Integer (Integer.
parseInt(rowStr));
int col = new Integer (Integer.
parseInt(colStr));
ArrayList finishBoard = new ArrayList();
finishBoard = board.updateBoard(col, row);
System.out.println(finishBoard);
ooStream.writeObject(finishBoard);
out.println(ooStream);
System.out.println("done sending");
//ooStream.close();
System.out.println("in2" + inputLine);
//inputLine = null;
inputLine = in.readLine();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
These are the errors:
Server:
Code:
run:
Hi, I'm the TCP server!
58554
in10,1
(correct arraylist display was here)
done sending
in20,1
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at networkTCP.tcpserverthread.run(tcpserverthread.java:53)
at networkTCP.tcpserver.serverstart(tcpserver.java:26)
at networkTCP.tcpserver.main(tcpserver.java:33)
BUILD STOPPED (total time: 12 seconds)
TCP CLIENT:
Code:
run:
pressed
sent coords: 0,1
Still alive?
Still alive?
waiting for board object to be returned!
board object returned
pressed
Couldn't get I/O for the connection to: 127.0.0.1.
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
any help is greatly appreciated.