Solved

Connect to a server

Posted on 2002-04-18
14
235 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 214
trigs fail! I thought I knew how to do trignometry 3 65
Issues with C++ Class 19 92
Precision Problem in C++ 7 31
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

785 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