Solved

Blocking Sockets giving problem

Posted on 2000-03-24
16
254 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:highmarks
16 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 2652701
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
 
LVL 32

Expert Comment

by:jhance
ID: 2652702
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
 
LVL 2

Author Comment

by:highmarks
ID: 2652947
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
 
LVL 32

Expert Comment

by:jhance
ID: 2653778
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
 
LVL 2

Author Comment

by:highmarks
ID: 2654419
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
 
LVL 8

Expert Comment

by:Brain2000
ID: 2654471
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
 
LVL 4

Expert Comment

by:wylliker
ID: 2655518
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
 
LVL 32

Expert Comment

by:jhance
ID: 2655821
>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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Author Comment

by:highmarks
ID: 2656185
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
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 2656320
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
 
LVL 2

Author Comment

by:highmarks
ID: 2656381
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
 
LVL 2

Author Comment

by:highmarks
ID: 2656384
JHANCE
should I send you my code for server(i.e service) and client at
expert@bysnet.com
0
 
LVL 32

Expert Comment

by:jhance
ID: 2656914
If you want to send the code, I'll be happy to look at it.
0
 
LVL 2

Author Comment

by:highmarks
ID: 2657008
ok will send you on Monday
0
 
LVL 2

Author Comment

by:highmarks
ID: 2857896
Comment accepted as answer
0
 
LVL 2

Author Comment

by:highmarks
ID: 2857897
Altough I never got the answer but you were very close to it.
Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now