0

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>  */

#define WIN32_LEAN_AND_MEAN
#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] = "10.36.7.172";
  char   AddrString[256]  = "Some dummy value";
  DWORD   dwSizeOfStr = sizeof(AddrValue);
  int    nSizeOfInput = sizeof(AddrString);
  WSADATA wsaData;
  SOCKET ConnectSocket = INVALID_SOCKET;
  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("10.36.7.172");
  //inet_pton(AF_INET,"10.36.7.172",&servaddr.sin_addr);
  if(WSAStringToAddressA(AddrValue, AF_INET, NULL, (LPSOCKADDR)&servaddr, &nSizeOfInput) != 0)
  {
    printf("\nWSAStringToAddressA failed with error num %ld\n", WSAGetLastError());
    WSACleanup();
    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());
    WSACleanup();
    return 1;
  }
  
  iResult = connect(ConnectSocket, (LPSOCKADDR)&servaddr, sizeof(struct sockaddr));
  if (iResult == SOCKET_ERROR) {
    closesocket(ConnectSocket);
    ConnectSocket = INVALID_SOCKET;
    return;
  }

  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 );
  sleep(5);
  closesocket(ConnectSocket);
  WSACleanup();
  system("PAUSE");
  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.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by cool_zephyr
0

may be this should work
servaddr.sin_addr.s_addr=inet_addr(AddrValue);

check the winsock2.h header file for definitions

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.