Solved

Winsock Control

Posted on 2000-04-27
10
175 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
 
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
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 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

21 Experts available now in Live!

Get 1:1 Help Now