Results 1 to 2 of 2
Thread: TCP sockets problem
- 10-18-2009, 04:10 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
TCP sockets problem
Hello, I would like to ask you guys for help. We are developing small application to school and I have problem with TCP communication.
I am sending packets from client to server in loop. But I always get same errors - Software caused connection abort: socket write error or Software caused connection abort: socket recv failed.
Here is code of client:
Socket oSocket;
InputStream oInput;
OutputStream oOutput;
byte[] bBuffer = new byte[1];
byte[] bBuffera = new byte[10];
double result;
// Address of server
InetAddress oAddress;
oAddress = null;
// Obtain address of server
try {
oAddress = InetAddress.getByName(getsServerAddress());
} catch (UnknownHostException e2)
{
return -1;
}
// Create socket
try
{
oSocket = new Socket(oAddress, getiServerPort());
oInput = oSocket.getInputStream();
oOutput = oSocket.getOutputStream();
}
catch (Exception e)
{
return -1;
}
for (int i = 0; i < getiMaxLoop(); i++)
{
try
{
// HERE IS PROBLEM //
oOutput.write(bBuffer);
oInput.read(bBuffera);
}
catch (Exception e)
{
System.out.println("Unable to get data.");
System.out.println(e.getMessage());
return -1;
}
}
//Close datasocket
try
{
oSocket.close();
}
catch (IOException e)
{
System.out.println("Problem with closing socket.");
}
Here is server:
ServerSocket ss;
Socket so;
InputStream is;
OutputStream os;
try {
ss = new ServerSocket(port);
} catch (Exception e) {
System.out.println("No se puede asociar el puerto " + port +
" al socket TCP.");
return;
}
while (true) {
try {
so = ss.accept();
is = so.getInputStream();
os = so.getOutputStream();
int ch;
ch = is.read();
if ( dur > 0 )
wait(dur);
os.write(65);
so.close();
} catch (IOException e) {
System.out.println("Algo fue mal");
} catch (InterruptedException e) {
System.out.println("Adiós");
}
}
Any ideas why it is not working? Thanks
- 10-30-2009, 05:06 AM #2
I think the problem is the bBuffer is null. Declaring a byte[] b = new byte[1] allocates an array object to store a byte, but does not set the contents. you would also need b[0] = SOME_BYTE_VALUE.
also, shouldn't close() the outut stream in the server, in this example, the client invokes a write within the for loop, so it will work the 1st time and fail the second time.
but also, the way the client works is to
open connection to server
for i ... in a loop {
send
read
}
and the server is only
for (ever) {
wait for client connection
read 1 byte
write 1 byte
/ close connection
}
So what we would need to make it work is either
- have the client expect to re-connect after writing/reading, because the server thinks it's done with the connection after reading/writing once.
- have the server loop inside that existing while(true) loop after it has connected to have it echo forever, until tthe client closes its connection.
oh, and also.
- did not post in code tags, or code is not nicely formated.
- did not post complete source file, harder to try out the what it was to see what's wrong.
- not sure why in server you would read ch, but always write back "65" ?
Here is a complete class as two junit 4 test cases in the same class, such as to launch each from within eclipse, or other IDE. its helpful to do both at once (like 2 execute tests, to be able to step over it if needed).os.write(65);
What the server does is is a loop until input stream connection is closed (reads -1, or throws io exception). i also reversed the while() and try..catch so as to have the server exit on error of accept().
Try running the server test case, and the client one while server is running on localhost.Java Code:import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import org.junit.Test; public class TestTcpSockets { String serverAddress = "localhost"; int serverPort = 6666; int maxLoop = 10; long pauseTime = 2000; // milliseconds public int getMaxLoop() { return maxLoop; } public void setMaxLoop(int maxLoop) { this.maxLoop = maxLoop; } public String getServerAddress() { return serverAddress; } public void setServerAddress(String serverAddress) { this.serverAddress = serverAddress; } public int getServerPort() { return serverPort; } public void setServerPort(int serverPort) { this.serverPort = serverPort; } @Test public void client() { Socket oSocket; InputStream oInput; OutputStream oOutput; byte[] bBuffer = new byte[1]; byte[] bBuffera = new byte[1]; // no sense having this more than 1, server only sends 1 byte // Address of server InetAddress oAddress; oAddress = null; // Obtain address of server try { oAddress = InetAddress.getByName(getServerAddress()); } catch (UnknownHostException e2) { return; } // Create socket try { oSocket = new Socket(oAddress, getServerPort()); oInput = oSocket.getInputStream(); oOutput = oSocket.getOutputStream(); } catch (Exception e) { System.err.println(e); e.printStackTrace(); return; } for (int i = 0; i < getMaxLoop(); i++) { try { bBuffer[0] = String.valueOf(i).getBytes()[0]; // jam some data in there. // HERE IS PROBLEM System.out.println("sending value " + (i+1) + " of " + getMaxLoop() + " to server: " + (char)bBuffer[0]); oOutput.write(bBuffer); // you didn't have flush here. oOutput.flush(); oInput.read(bBuffera); System.out.println("response " + (i+1) + " of " + getMaxLoop() + " from server: " + (char) bBuffera[0]); } catch (Exception e) { System.out.println("Unable to get data."); System.out.println(e.getMessage()); return; } } // Close datasocket try { oSocket.close(); } catch (IOException e) { System.out.println("Problem with closing socket."); } } @Test public void server() { ServerSocket ss; Socket so; try { ss = new ServerSocket(getServerPort()); } catch (Exception e) { System.out.println("unable to listen on TCP port " + getServerPort() + ", already in use."); return; } // I reversed the order of the while and try..catch.. so if an error occurs the server exits, as is ok because this is a test right. try { while (true) { System.out.println("server waiting for a connection from a client."); so = ss.accept(); System.out.println("client connected from " + so.getRemoteSocketAddress()); // begin handler for one connected client InputStream is = null; OutputStream os = null; try { is = so.getInputStream(); os = so.getOutputStream(); int ch; // as long as stuff can be read from the client ( e.g. socket, and stream are open). while ( (ch = is.read()) > -1) { System.out.println("received: " + (char)ch); try { Thread.sleep(pauseTime); } catch (InterruptedException ex) { } System.out.println("echoing character " + (char) ch + " back to client."); os.write(ch); // you didn't have flush os.flush(); // should not close here, the client should close when it's done. // or, at least, we need to be aware that closing it here will cause the client to exit with closed pipe error // so.close(); } // while (connected) } catch (IOException ex) { // the client has closed the connection. } // and you didn't have the cleanup the open sockets part finally { if (is != null) { try { is.close(); } catch (IOException ex) {} } if (os != null) { try { os.close(); } catch (IOException ex) { } } } } // while (server) } catch (IOException e) { // in case there was errors in acept(), break the while loop. System.out.println("Socket error: " + e.getMessage()); e.printStackTrace(); } } }
Client output:
Server output:Java Code:sending value 1 of 10 to server: 0 response 1 of 10 from server: 0 sending value 2 of 10 to server: 1 response 2 of 10 from server: 1 sending value 3 of 10 to server: 2 response 3 of 10 from server: 2 sending value 4 of 10 to server: 3 response 4 of 10 from server: 3 sending value 5 of 10 to server: 4 response 5 of 10 from server: 4 sending value 6 of 10 to server: 5 response 6 of 10 from server: 5 sending value 7 of 10 to server: 6 response 7 of 10 from server: 6 sending value 8 of 10 to server: 7 response 8 of 10 from server: 7 sending value 9 of 10 to server: 8 response 9 of 10 from server: 8 sending value 10 of 10 to server: 9 response 10 of 10 from server: 9
Java Code:server waiting for a connection from a client. client connected from /127.0.0.1:43705 received: 0 echoing character 0 back to client. received: 1 echoing character 1 back to client. received: 2 echoing character 2 back to client. received: 3 echoing character 3 back to client. received: 4 echoing character 4 back to client. received: 5 echoing character 5 back to client. received: 6 echoing character 6 back to client. received: 7 echoing character 7 back to client. received: 8 echoing character 8 back to client. received: 9 echoing character 9 back to client. server waiting for a connection from a client.
Similar Threads
-
Problem (sockets)
By Blacknight962 in forum New To JavaReplies: 3Last Post: 08-16-2009, 12:56 AM -
Sockets and JFrames problem
By marcg11 in forum Advanced JavaReplies: 6Last Post: 08-15-2009, 01:07 PM -
Sockets NIO
By aamp in forum New To JavaReplies: 3Last Post: 01-15-2009, 10:56 AM -
Sockets
By Zosden in forum NetworkingReplies: 16Last Post: 05-27-2008, 04:55 PM -
Help with Sockets
By Eric in forum NetworkingReplies: 3Last Post: 12-01-2007, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks