Winsock Control

What Visual C++ 6.0 command do I use to communicate between computers in a network using the computer port?I am using win32 socket function.My OS is Windows 98.
spideymanAsked:
Who is Participating?
 
Brain2000Connect With a Mentor Commented:
You'll need an application on both sides.  One will listen for the other to connect.

Here's some sample code on how to listen for a socket.  Note, call the init_server_side() function first, then call wait_for_connect() and it will return a SOCKET when you connect to it.

Once you're connected, you can use the following two functions to send and receive data with the SOCKET.

recv(hSocket,inbuffer,bytes,0);
send(hSocket,outbuffer,bytes,0);

SERVER SOURCE SAMPLE
-----------------------
const    VERSION_MAJOR  = 2;
const    VERSION_MINOR  = 0;
const    PORT_NUMBER    = 2222;
WSADATA        WSData;
SOCKADDR_IN    SockAddr;
SOCKADDR_IN    incomingSockAddr;
LPHOSTENT      lpHostEntry;
SOCKET         hServer=INVALID_SOCKET;
SOCKET         newSocket=INVALID_SOCKET;
int            iProtocolPort;
int            junk;

BOOL init_server_side(void)
{
  char temp[1000];

  if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
  {
    printf("Cannot find Winsock v%d.%d or later\n",VERSION_MAJOR,VERSION_MINOR);
    return(0);
  }

  gethostname(temp,sizeof(temp));
  lpHostEntry=gethostbyname(temp);
  if(lpHostEntry == NULL)
  {
    WSACleanup();
    return(0);
  }

  // Create a socket.
  hServer = socket(PF_INET, SOCK_STREAM, 0);
  if(hServer == INVALID_SOCKET)
  {
    printf("Cannot create socket\n");
    WSACleanup();
    return(0);
  }
  iProtocolPort = htons(PORT_NUMBER);

  // Setup a Socket Address structure.
  SockAddr.sin_family = AF_INET;
  SockAddr.sin_port = iProtocolPort;
  SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);

  //now bind the socket
  if(bind(hServer,(const struct sockaddr FAR *)&SockAddr,sizeof(SockAddr)))
  {
    closesocket(hServer);
    hServer=INVALID_SOCKET;
    WSACleanup();
    printf("Error binding socket\n");
    return(0);
  }

  //we're going to listen
  if(listen(hServer,SOMAXCONN))
  {
    printf("Error listening to Server socket.\n");
    closesocket(hServer);
    hServer=INVALID_SOCKET;
    WSACleanup();
    return(0);
  }
  return(1);
}

SOCKET wait_for_connect(void)
{
  char szServerIP[20];

  if(hServer==INVALID_SOCKET) {
    printf("SOMETHING BAD HAPPENED TO OUR SOCKET...\n");
    return(INVALID_SOCKET);
  }

  junk=sizeof(incomingSockAddr);

  //this will block until someone connects to us
  newSocket=accept(hServer,(struct sockaddr FAR *)&incomingSockAddr,&junk);

  if(newSocket==INVALID_SOCKET)
  {
    closesocket(hServer);
    hServer=INVALID_SOCKET;
    WSACleanup();
    printf("Error accepting Socket Connection.\n");
    return(INVALID_SOCKET);
  }

  SockAddr.sin_addr = incomingSockAddr.sin_addr;

  sprintf(szServerIP,inet_ntoa(incomingSockAddr.sin_addr));
  printf("Successful Connect To %s\n",szServerIP);

  return(newSocket);
}
0
 
Brain2000Commented:
Forget to mention something.  The client connects to the server using the connect() function.  Here's a sample for the client to use to connect to the server:


SOCKET client_side(char *szServerNameOrIP)
{
  char temp[1000];
  SOCKET hSocket;

  if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
  {
    printf("Cannot find Winsock v%d.%d or later\n",VERSION_MAJOR,VERSION_MINOR);
    return(0);
  }

  lpHostEntry=gethostbyname(szServerNameOrIP);
  if(lpHostEntry == NULL)
  {
    WSACleanup();
    return(0);
  }

  // Create a socket.
  hSocket = socket(PF_INET, SOCK_STREAM, 0);
  if(hSocket == INVALID_SOCKET)
  {
    printf("Cannot create socket\n");
    WSACleanup();
    return(0);
  }
  iProtocolPort = htons(PORT_NUMBER);

  // Setup a Socket Address structure.
  SockAddr.sin_family = AF_INET;
  SockAddr.sin_port = iProtocolPort;
  SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);

//note that the client doesn't need to bind() or listen(), just connect()

  // Connect the Socket.
  if(connect(hSocket, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
  {
    printf("Error connecting to Server socket...");
    closesocket(hSocket);
    WSACleanup();
    return(0);
  }
  printf("CONNECTED!\n");
  return(hSocket);
}
0
 
xelpmoc2Commented:
Is this suppose to be a socket application?  Are you using ethernet? TCP/IP?  IF you are going to create a socket application I would suggest not using the code above, it is in C, there are mover advanced calls for Visual C++.  Do you have MSDN?  
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Brain2000Commented:
Not an application in particular.  I posted a snippet of sample code for a socket application.  Yes I'm using ethernet.  Yes I'm using TCP/IP.  The calls above can be used in either a C or C++ environment.  When put together into an app, it compiles into a 32 bit app with Visual C++.  There are no more advanced calls in Visual C++ (unless you want to use WM_ message pump, but I'm keeping this simple to begin with and using standard Win32 socket functions to demonstrate the functionality and not to confuse any poor chaps)

MSDN explains the calls above in greater detail, like socket(), bind(), listen(), accept(), connect(), recv(), and send()
0
 
spideymanAuthor Commented:
Edited text of question.
0
 
pellepCommented:
I have to agree with Brain2000. The 'more advanced' calls referred to I suppose is the MFC CSocket class, which I find to be not very flexible and, as most MFC stuff, not suited for beginners. I've written socket-programs for quite some years and I still think that the 'ol winsock API works best.
spideyman - go with Brain2000 and you won't be disappointed.
0
 
Brain2000Commented:
Haven't heard from spidey in a while...
0
 
spideymanAuthor Commented:
Adjusted points from 100 to 200
0
 
spideymanAuthor Commented:
Thank you so much for your help...
I'll try your sample codes...
Maybe next time again...
Thanks again...
0
 
Brain2000Commented:
Ok, let me know if you have any problems putting together or running a program based on the samples above.  I'll help where I can.
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.