• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connection timed out in java

 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear sir,


i have implemented client and server code.my client code is java code and server is c code.when i send some no of bytes to ,its get
connection timed in out in socket.write method. i am using windows xp.


try
{
Socket socket=new Socket(serverIP,serverport);
System.out.println("socket connection");
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();

System.out.println("send some data");
socket.setSoTimeout(600000);
for(i=0;i<12;i++)
{
out.write(sendbuffer[i]);
}

System.out.println("write success");
socket.setSoTimeout(600000);
in.read(recvbuffer);
// readbuffer(recvbuffer);
System.out.println("read success");
out.close();
in.close();
socket.close();

return 1;
}catch(IOException e)
{

System.out.println(e);
return 0;

// JOptionPane.showMessageDialog(null,e);

}
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that the server side C code is working fine? I believe you are getting timed out while writing to server. Possibly your can use a network analyzer to see, whats going down the wire.

Additionally, why are you calling setSOtimeout twice. Calling it just before read is good enough.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh, please use code tags.
 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear sir,

private static void starttxrxpacket()
{

new Thread()
{
@Override
public void run ()
{
while (true)
{
synchronized (this) {
try {
wait(100);
recvrxtxpacket(recvbuffer);
}
else
{

System.out.println("connection failed");
connectionfail();
continue;
}

} catch (Exception e) {

System.out.println("in thread process" +e);

}
}




}
}
}.start();
}

this function contiously read data from server.this function is working fine some system,but some systems its throwing
connection timed out exception in socket.read() method.how to solve the error "connection timed out problem" my client code is
java its develped in swing gui and my server code is c code.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't I ask you to use code tags? Also, please post real code. There is an else without a matching if in there.
 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear sir,

private static void starttxrxpacket()
{

new Thread()
{
@Override
public void run ()
{
while (true)
{
synchronized (this) {
try {
wait(100);
recvrxtxpacket(recvbuffer); this function used to connect host and port and read the data and store in recvbuffer
}

} catch (Exception e) {


}
}




}
}
}.start();
}

this function contiously read data from server.this function is working fine some system,but some systems its throwing
connection timed out exception in socket.read() method.how to solve the error "connection timed out problem" my client code is
java its develped in swing gui and my server code is c code.
 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear sir,

private static void starttxrxpacket()
{

new Thread()
{
@Override
public void run ()
{
while (true)
{
synchronized (this) {
try {
wait(100);
recvrxtxpacket(recvbuffer); this function used to connect host and port and read the data and store in recvbuffer
}

} catch (Exception e) {


}
}




}
}
}.start();
}

this function contiously read data from server.this function is working fine some system,but some systems its throwing
connection timed out exception in socket.read() method.how to solve the error "connection timed out problem" my client code is
java its develped in swing gui and my server code is c code.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you won't bother adding code tags, even though I've asked you twice, then I won't bother helping you.
 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this function contiously read data from server in real time process after 6 hours error generted is connection timed out in socket.write method .how solve the error "connection timed out problem" my client code is
java its develped in swing gui and my server code is c code. please help me how to slove connection timed out problem




Rob Prime wrote:.
 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhosh babu el wrote:

this function contiously read data from server in real time process after 6 hours error generted is connection timed out in socket.write method .how solve the error "connection timed out problem" my client code is
java its develped in swing gui and my server code is c code. please help me how to slove connection timed out problem




Rob Prime wrote:.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that the server was up when the error occurred?
Is it possible to provide the exception stack trace?
Additionally, you should be closing all the open io streams in the finally block.
 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya Maheshwari wrote:Are you sure that the server was up when the error occurred?
Is it possible to provide the exception stack trace?
Additionally, you should be closing all the open io streams in the finally block.




yes the server was up when error occurred.i dont understand where i should put finally block.and how we can find solution by putting exception stack trace
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how you could use the finally block:


The exeption stack trace will not give us a solution but may throw some light on the problem.
 
santhosh babu el
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya Maheshwari wrote:Here's how you could use the finally block:


private void startrxtxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

starttxrxpacket();


}

private static void starttxrxpacket()
{

new Thread()
{
@Override
public void run ()
{
while (true)
{

synchronized (this) {
try {
wait(100);
status= recvrxtxpacket(recvbuffer);
if(status==1)
{
maskval1(recvbuffer, resultbuff);
Thread.sleep(1000);
}
} catch (Exception e) {


}
}




}
}
}.start();
}

private static int recvrxtxpacket(byte[] recvbuffer)
{
int i=0,j=0;
int serverport=3005;
String serverIP=iptext.getText();
OutputStream out =null;
InputStream in=null;
int sendbuffer[]=new int[14];

sendbuffer[0]=0x55;
sendbuffer[1]=0x55;
sendbuffer[2]=0xFF;
sendbuffer[3]=0xFF;
sendbuffer[4]=0x01;
sendbuffer[5]=0x05;
sendbuffer[6]=0x00;
sendbuffer[7]=0x01;
sendbuffer[8]=0x00;
sendbuffer[9]=0x01;

sendbuffer[10]=0xAA;
sendbuffer[11]=0xAA;

try
{
Socket socket=new Socket(serverIP,serverport);
System.out.println("socket connection");
out = socket.getOutputStream();
in = socket.getInputStream();

System.out.println("send some data");
socket.setSoTimeout(600000);
for(i=0;i<12;i++)
{
out.write(sendbuffer[i]);
}

System.out.println("write success");
socket.setSoTimeout(600000);
in.read(recvbuffer);
// readbuffer(recvbuffer);
System.out.println("read success");
out.close();
in.close();
socket.close();

return 1;
}catch(Exception e)
{

System.out.println(e);
e.printStackTrace();
return 0;

// JOptionPane.showMessageDialog(null,e);

} finally
{
if((in!=null) && (out!=null))
{

in.close();
out.close(0;
}
}
}





madam i have make changes what you have posted but still my problem exist. i am error liseed beloe.please help me to slove these proble.i am having this problem for past 6 months

java.net.ConnectException: Connection timed out: connect
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at beam.sendack1(beam.java:6520)
at beam.Process_beam1(beam.java:4868)
at beam.access$2100(beam.java:41)
at beam$LocalXYDataset$1.run(beam.java:3529)

please help me



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic