Solved

Winsock Control

Posted on 2000-04-27
10
187 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:spideyman
10 Comments
 
LVL 8

Accepted Solution

by:
Brain2000 earned 200 total points
ID: 2756015
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
 
LVL 8

Expert Comment

by:Brain2000
ID: 2756044
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
 
LVL 1

Expert Comment

by:xelpmoc2
ID: 2756139
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
Technology Partners: 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 8

Expert Comment

by:Brain2000
ID: 2756309
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
 

Author Comment

by:spideyman
ID: 2757586
Edited text of question.
0
 
LVL 4

Expert Comment

by:pellep
ID: 2769337
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
 
LVL 8

Expert Comment

by:Brain2000
ID: 2769589
Haven't heard from spidey in a while...
0
 

Author Comment

by:spideyman
ID: 2771900
Adjusted points from 100 to 200
0
 

Author Comment

by:spideyman
ID: 2771901
Thank you so much for your help...
I'll try your sample codes...
Maybe next time again...
Thanks again...
0
 
LVL 8

Expert Comment

by:Brain2000
ID: 2774201
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

Featured Post

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!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

685 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