Hi all. I'm trying to develop a Winsock program in C. I can already compile the program correctly but I don't get any output when I run it. I don't know how to exactly use the WSAStringToAddressA function so I think it's the root of my problem. I'm using this function because I already know the IPv4 address of the other machine I'm connecting to and I just need to convert this address to the number format understandable by machines. I don't know now how to debug my program so kindly help me out. Thanks in advance!

/*  #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>  */

#include <windows.h>
//#include <winsock.h>
#include <winsock2.h>
#include <stdio.h>
#include <string.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib") 

int main(int argc, char **argv) {
  SOCKADDR_IN servaddr;
 // socklen_t len = sizeof(servaddr);
  char   AddrValue[256] = "";
  char   AddrString[256]  = "Some dummy value";
  DWORD   dwSizeOfStr = sizeof(AddrValue);
  int    nSizeOfInput = sizeof(AddrString);
  WSADATA wsaData;
  int iResult;

  // Initialize Winsock
  iResult = WSAStartup(MAKEWORD(1,1), &wsaData);
  if (iResult != 0) {
      printf("WSAStartup failed with error: %d\n", iResult);
      return 1;

  ZeroMemory(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(80);
//  servaddr.sin_addr.ConnectSocket_addr = inet_addr("");
  if(WSAStringToAddressA(AddrValue, AF_INET, NULL, (LPSOCKADDR)&servaddr, &nSizeOfInput) != 0)
    printf("\nWSAStringToAddressA failed with error num %ld\n", WSAGetLastError());
    return 1;

  ConnectSocket = socket(PF_INET, SOCK_STREAM, 0);
  //ConnectSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (ConnectSocket == INVALID_SOCKET) {
    printf("socket failed with error: %ld\n", WSAGetLastError());
    return 1;
  iResult = connect(ConnectSocket, (LPSOCKADDR)&servaddr, sizeof(struct sockaddr));
  if (iResult == SOCKET_ERROR) {
    ConnectSocket = INVALID_SOCKET;

  char x1[] = "This is msg 1\n";
  send( ConnectSocket, x1, strlen(x1), 0 );
  char x2[] = "This is msg 2\n";
  send( ConnectSocket, x2, strlen(x2), 0 );
  return 0;

I'm also not sure how to use the socket() function in windows. I'm not sure if I should put IPPROTO_TCP as the third parameter or simply 0.

may be this should work

check the winsock2.h header file for definitions

This article has been dead for over six months. Start a new discussion instead.