Results 1 to 1 of 1
- 11-25-2011, 02:41 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
How to make socket connection continue
Hi I want to make a calculator with server, so after I click the numbers and the operator, the two numbers and the operator will be sent to the server.
And in server the calculation will be done and then the result is send back to the client.
But the code I made just can do the calculation one time, and it will disconnect. I want it to be always connect so I can do another calculation.
And this is a part of my code (in the client side (GUI)):
and this is in the server side:Java Code:ConSocket cs = new ConSocket(); private void jButtonEqualsActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: number2 = reader(); try { cs.sock = new Socket("localhost", 10005); cs.oos = new ObjectOutputStream(cs.sock.getOutputStream()); cs.ois = new ObjectInputStream(cs.sock.getInputStream()); if(addc>0) { SendData sd = new SendData(number1, number2, "+"); cs.oos.writeObject(sd); cs.oos.flush(); result = (Double)cs.ois.readDouble(); text.setText(Double.toString(result)); }...
I've tried to use setKeepAlive(true), but I didn't see any difference. So any help would be greatly appreciated ^^Java Code:public static void main(String[] args) { Socket sock = null; ObjectOutputStream oos = null; ObjectInputStream ois = null; try { ServerSocket ss = new ServerSocket(10005); System.out.println("클라이언트의 접속에 대기합니다."); sock = ss.accept(); oos = new ObjectOutputStream(sock.getOutputStream()); ois = new ObjectInputStream(sock.getInputStream()); Object obj = null; keepConnect: while(true){ obj = ois.readObject(); SendData sd = (SendData) obj; double number1 = sd.getNumber1(); double number2 = sd.getNumber2(); String opcode = sd.getOpcode(); if(opcode.equals("+")) { oos.writeDouble(number1+number2); oos.flush(); }... System.out.println("결과를 전송했습니다."); sock = ss.accept(); break keepConnect; } }
Thank you
Similar Threads
-
Java.net.socket connection :connection closed
By veeru541 in forum Advanced JavaReplies: 2Last Post: 06-27-2010, 02:14 AM -
Socket connection Problem
By ighor10 in forum NetworkingReplies: 3Last Post: 06-23-2010, 04:10 AM -
Socket Connection problem
By xpan in forum NetworkingReplies: 4Last Post: 02-14-2010, 04:32 PM -
Socket connection - MIDlet
By arun10427 in forum CLDC and MIDPReplies: 2Last Post: 01-26-2010, 02:51 AM -
How To Make Socket Connection
By madhumurundi in forum NetworkingReplies: 5Last Post: 04-21-2008, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks