Results 1 to 5 of 5
Thread: Keep my TCP connection alive
- 02-25-2009, 05:08 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
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
Java 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
Java 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
Java 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:
Java 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:
Java 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.
- 03-01-2009, 05:32 AM #2
Why are you using localhost (127.0.0.1) on both sides? This makes zero sense.
- 03-01-2009, 12:19 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
I'm not anymore
I have to callJava Code:public void setupConnection(String IPAddy2) throws Exception { InetAddress IPAddy = InetAddress.getByName(IPAddy2); // keep connection alive here echoSocket = new Socket(IPAddy, 2323); }every time i need to send somethingJava Code:setupConnection("10.0.0.1");
- 03-01-2009, 06:01 PM #4
Try debugging the code on one side, and then the other. See what looks funny.
I'm guessing the client closes the connection after the first pass, and the server gets an error message.
- 03-02-2009, 06:11 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
This is a definite problem, though not necessarily the only one.
When you close the ObjectInputStream, it closes it's underlying resources as well, which includes your Socket's InputStream.Java Code:ois.close();
If you are planning to reuse the same underlying InputStream, you do not need to close any streams wrapped around it (such as ObjectInputStream). Just let them go out of scope.
Similar Threads
-
connection problem
By subash in forum JDBCReplies: 5Last Post: 04-22-2008, 09:17 AM -
How to configure "keep alive" in Sockets?
By rajdotme in forum NetworkingReplies: 1Last Post: 04-16-2008, 11:36 PM -
no connection
By even in forum JDBCReplies: 15Last Post: 01-02-2008, 01:50 PM -
JDBC connection
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 07:39 PM -
connection pool for db2
By paty in forum JDBCReplies: 1Last Post: 08-06-2007, 02:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks