Solved

Connect to a server

Posted on 2002-04-18
14
236 Views
Last Modified: 2010-08-05
How do I connect to an X400 server? My application needs to connect to the server - check if it is up and then disconnect.
0
Comment
Question by:JennyP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
14 Comments
 

Expert Comment

by:adamretter
ID: 6950812
Firstly what protocols are running on your network, what protocols does your x400 server support and what platform are you programming your checking application on.

If TCP/IP is available then you could simply create a small c++ program that does an ICMP ping off of the x400 server to tell if it is switched on and responding to network requests.
0
 

Author Comment

by:JennyP
ID: 6950857
TCP/IP is available. I will need to check if the server is up under 2 conditions:
1. ISDN modem dial up
2. over a WAN
How would I do a ping? Simple C++ code that I can include in my program will be great. I will increase points if required.

0
 

Expert Comment

by:adamretter
ID: 6951031
Two Options
===========

1.)
You need to Use RAW sockets in Windows as far as I can understand.
These are only available under windows 2000/xp and must be executed by someone with Administrator privelledges.

Would this be of any use?

2.) Redirect C++ Standard I/O pipes so that you can simply call ping.exe the dos cmd and use that.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:lsmgms
ID: 6952634
Hi,

How about makeing SNMP request to query system up time?  Assume you can get the community name.
0
 

Author Comment

by:JennyP
ID: 6952650
I don't think RAW sockets will be of use 'cos this app will run of various OS Win 9x, Win 2000, etc.

Don't have the community name so I am not sure a SNMP request will work.

How would I redirect Standard I/O pipes? How will I be able to intepret the success or fail from the dos ping command ? I know it returns a reply...but how can I read that from my C++ app?

0
 
LVL 1

Expert Comment

by:lsmgms
ID: 6952690
Please look at this:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q190351

However I myself have not tried before.  Are you sure you want to do this because it is very urgly, for me.

You do not need to have full access right community name, just read-only is good enough.
0
 

Author Comment

by:JennyP
ID: 6952712
I am not sure I want to do that. What would you suggest as an alternative? I am looking for a simple way to connect to an X400 server (I have the IP address and port) to check if it is up and running. The function must return a value for my app to interpret if the server is up or down.
0
 
LVL 1

Expert Comment

by:lsmgms
ID: 6952726
>> (I have the IP address and port)

Is it a TCP port? Can you just open a socket and then connect to that IP and port and immediately close it? If socket is opened successfully, we can confirm that server is on.
0
 

Author Comment

by:JennyP
ID: 6952734
Yes, it is TCP/IP. What would I use CSocket or similar ? And how ? Excuse my ignorance.
0
 
LVL 1

Expert Comment

by:lsmgms
ID: 6952746
- Open a blocking socket
- Call Connect()
- Check return value whether success or not.
- Close the socket.

Either CSocket or WINSOCK API will do.  
0
 

Author Comment

by:JennyP
ID: 6952768
Do we not need to pass any username & password?
0
 
LVL 1

Accepted Solution

by:
lsmgms earned 50 total points
ID: 6952820
I think you are new to socket, try the example below.  Replace params in IsAlive("127.0.0.1",23) with your server and port.  It does not need any username, so long as the firewall allows you to do that.  I hope this has enough info for you to solve the problem.




#include <windows.h>
#include <winsock.h>
#include <stdio.h>

int IsAlive(char *szServer, short nPort)
{
     int alive =0;

     SOCKET     theSocket;

     theSocket = socket(AF_INET,                    // Address family
                            SOCK_STREAM,               // Socket type
                            IPPROTO_TCP);          // Protocol
     if (theSocket == INVALID_SOCKET)
     {
          printf("Open socket error %ld", WSAGetLastError());
          return 0;
     }

     //
     // Fill in the address structure
     //
     SOCKADDR_IN saServer;

     saServer.sin_family = AF_INET;
     saServer.sin_addr.S_un.S_addr  = inet_addr(szServer);
     saServer.sin_port = htons(nPort);    

     //
     // connect to the server
     //
     if( connect(theSocket,                    
                       (struct sockaddr*)&saServer,    
                       sizeof(struct sockaddr)) != SOCKET_ERROR      
                          )
          alive = 1;
     else
          printf("Connect error %ld", WSAGetLastError());

     closesocket(theSocket);

     return alive;
}


int main(int argc, char* argv[])
{
     WORD wVersionRequested = MAKEWORD(1,1);
     WSADATA wsaData;
     int nRet;


     nRet = WSAStartup(wVersionRequested, &wsaData);
     if (wsaData.wVersion != wVersionRequested)
     {    
          fprintf(stderr,"\n Wrong version\n");
          return 0;
     }


     //
     // Go do the stuff a stream client does
     //
     if( IsAlive("127.0.0.1",23))
          printf("Alive");
     else
          printf("NOT Alive");

     
     //
     // Release WinSock
     //
     WSACleanup();


     return 1;
}
0
 
LVL 1

Expert Comment

by:lsmgms
ID: 6952857
Forgot to mention, you have to include Ws2_32.lib library to your project.
0
 

Author Comment

by:JennyP
ID: 6959472
This was great. thanks for the help !
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 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