• C

Blocking Sockets giving problem

Hi!
I am using vc++6.0 on Win-NT4.0
I have written a service in C which is installed on IP address 64.19.155.146(this is actual IP address where the service is still running on port 9896)
Now I have written a client which is basically a systray icon lying on my machine.
The function of this client is to connect to this service after every 30 seconds and check for some file present on that system and if yes then beep and change icon.
My problem is that when you try to ping this IP address 64.19.155.146 sometimes it gives request timed out and sometimes it tell reply.........
So in my code which is as follows

SOCKET sock;
struct sockaddr_in serv_addr;
if ( connect (sock , (struct sockaddr*) &serv_addr , sizeof (serv_addr) ) < 0 )
{
MessageBox(hwndMain,"Cannot connect to the server","ERROR",MB_ICONHAND) ;
}

Because of request timed out it is not able to connect to server(which is a service running) and gives me the above messagebox as "cannot connect to server"
so all furthur processing is also not possible.
I have tried this program on our LAN and it works perfect, but the problem involved is with m/c on that IP address.
Is there any way to set the time limit until which keep on trying to connect and also after connecting keep on trying to send and wait until anything is received.i.e. concept of blocking sockets in C.

If any of you wanna try to ping you can go ahead and at least ping 4-5 times to notice the actual difficulty.
Please give me source code also while give me the answer.
ThankYou.
LVL 2
highmarksAsked:
Who is Participating?
 
jhanceConnect With a Mentor Commented:
I'm telling you that either some router or port filter on this system or network is blocking these ports or your program is refusing to talk to "foreign" hosts.  I still assuming that your code is basically correct since you said you could connect to this port from inside the local network.

Look:

[postgres@joe joe]$ telnet 64.19.155.146 7865
Trying 64.19.155.146...
telnet: Unable to connect to remote host: Connection refused

But if I try the SMTP port 25:

[postgres@joe joe]$ telnet 64.19.155.146 25
Trying 64.19.155.146...
Connected to 64.19.155.146.
Escape character is '^]'.
220-64.19.155.161 Microsoft SMTP MAIL ready at Sat, 25 Mar 2000 05:02:08 +0000 V
ersion: 5.5.1877.977.9
220 ESMTP spoken here

Port 80 also works to your web server:

[postgres@joe joe]$ telnet 64.19.155.146 80
Trying 64.19.155.146...
Connected to 64.19.155.146.
Escape character is '^]'.
GET / HTTP/1.0

HTTP/1.1 200 OK
Server: Microsoft-IIS/4.0
Date: Sat, 25 Mar 2000 05:03:19 GMT
Content-Type: text/html
Set-Cookie: ASPSESSIONIDGGQQGHDX=CGLDDNPAHDOMHOLHKMHEBDAM; path=/
Cache-control: private

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>

<head>
<meta name="postinfo" content="/scripts/postinfo.asp">
<title>Welcome To IIS 4.0!</title>
</head>

<body bgcolor="#FFFFFF" topmargin="0" leftmargin="0" LINK="#0080FF" VLINK="#0080
FF">
0
 
jhanceCommented:
Why not do this:

SOCKET sock;
struct sockaddr_in serv_addr;
while ( connect (sock , (struct sockaddr*) &serv_addr , sizeof (serv_addr) ) < 0 )
{
 Sleep(100);
}

Instead of displaying the message box which hangs the service.  It's almost never a good idea to use MessageBox in a service because you never know for sure that someone will be there to say OK.  A better approach is to write eny problems to the Event Log.

In the code above, you might want to consider a limit on the number of times it trys before giving up, depending on your requirements.
0
 
jhanceCommented:
BTW, I  pinged your IP address a number of times and never failed to get a response.  Maybe your network is just too busy at times.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
highmarksAuthor Commented:
jhance again highmarks
I will accept your comment as answer at later stage.
Also did u try to connect to port and ip address specified by me through telnet.
it gives host name failed. why?
and the method u specified will try to connect for 100 ms right? and if it succedds in that time it will go forward right?
And that messagebox is not in service but it is for client.
0
 
jhanceCommented:
I pinged your IP address and it works fine:

[root@joe /root]# ping 64.19.155.146
PING 64.19.155.146 (64.19.155.146): 56 data bytes
64 bytes from 64.19.155.146: icmp_seq=0 ttl=108 time=115.8 ms
64 bytes from 64.19.155.146: icmp_seq=1 ttl=108 time=211.1 ms
64 bytes from 64.19.155.146: icmp_seq=2 ttl=108 time=118.6 ms
64 bytes from 64.19.155.146: icmp_seq=3 ttl=108 time=186.6 ms
64 bytes from 64.19.155.146: icmp_seq=4 ttl=108 time=131.0 ms

--- 64.19.155.146 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 115.8/152.6/211.1 ms


I tried to connect with telnet like:

[root@joe /root]# telnet 64.19.155.146 9896
Trying 64.19.155.146...
telnet: Unable to connect to remote host: Connection refused

This message tells me that either this incoming IP port is blocked or that there is no service running to receive my connection.



0
 
highmarksAuthor Commented:
Hi!
Service is running I just checked 5 minutes back.
Something is blocking the port yes but what is that something which is blocking the port.

0
 
Brain2000Commented:
Why don't you post your service code.  Perhaps the issue lies there.  On another note, ping does not use sockets, so I don't see why ping would cease to work.
0
 
wyllikerCommented:
Is your firewall/proxy server allowing access to that specific port?

This will be a problem if you are trying to connect from outside of the firewall.

Pinging the server is one thing - opening a socket on a given port is another.
0
 
jhanceCommented:
>Something is blocking the port yes but
>what is that something which is
>blocking the port.

Obviously this server is behind a firewall of some sort.  Is this your site?  

Have you talked with the network administrator at this site?
0
 
highmarksAuthor Commented:
JHANCE
yes that is our site and talked to site administrator. There is no firewall or proxy server on this IP address.
I also tried changing port number also now it is on 7865.
Now this service on my m/c and lan works fine I can connect through telnet to port 7865.
BRAIN2000
do you really need code since the code works fine out here on our network.
Please think of something else.
0
 
highmarksAuthor Commented:
JHANCE
I managed to trapped the error which is,

WSAECONNREFUSED
(10061)
Connection refused.
No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.

Now can you suggest me something about this.
Here by netstat command I can see my service in listening mode so what else........ i am zapped.
0
 
highmarksAuthor Commented:
JHANCE
should I send you my code for server(i.e service) and client at
expert@bysnet.com
0
 
jhanceCommented:
If you want to send the code, I'll be happy to look at it.
0
 
highmarksAuthor Commented:
ok will send you on Monday
0
 
highmarksAuthor Commented:
Comment accepted as answer
0
 
highmarksAuthor Commented:
Altough I never got the answer but you were very close to it.
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.