client connect to the IP and Port that bind with server
server bind a IP and Port
then client connect the IP and Port
//client
/*if happend error: error C4996: 'inet_addr': Use inet_pton() or InetPton() instead
or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
Slove: Project properties -> Configuration Properties -> C/C++ -> General -> SDL checks -> No
*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define DES_IP "127.0.0.1"
#define DES_PORT 5000
int main()
{
SOCKET sock;
char buf[1024];
SOCKADDR_IN ser_addr;
WSADATA wsaData;
WORD wVersionRequired;
wVersionRequired = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequired, &wsaData)!= 0)
{
printf("init failed %ld\n",WSAGetLastError());
// return 1;
}
//create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
ser_addr.sin_addr.S_un.S_addr = inet_addr(DES_IP); //127.0.0.1
ser_addr.sin_port = htons(DES_PORT); //5000
ser_addr.sin_family = AF_INET;
memset(ser_addr.sin_zero, 0,