Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

You have the vaccine, now let everyone know!
Teach Your Child To Read

BlueHost Web Hosting Black Friday

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

17,954 questions

24,129 answers

573 users

How to create TCP client server socket communication on windows in C

1 Answer

0 votes
/*
You need to link the library: libws2_32.a 
OR
-lwsock32
-lws2_32
from c:\TDM-GCC-64\x86_64-w64-mingw32\lib32\
OR
c:\MinGW\lib\
if you using GCC on windows
*/
  
// Client and Server are on the same computer, with different exe files and two console windows 

// TCP Server program

#include <stdio.h>
#include <WinSock2.h>

int main() {
    printf("TCP SERVER\n");

    WSADATA Winsockdata;
    SOCKET TCPServerSocket;
    struct sockaddr_in TCPServerAddr;
    struct sockaddr_in TCPClientAddr;
    int TCPClientAddrSize = sizeof(TCPClientAddr);
    SOCKET AcceptSocket;
    char SendBuffer[512] = "Hello from Server!";
    char RecvBuffer[512];

    // STEP-1 WSAStartUp
    if (WSAStartup(MAKEWORD(2,2), &Winsockdata) != 0) {
        printf("SERVER: WSAStartUp Failed");
    }
    printf("SERVER: WSAStartUp Success\n");

    // STEP-2 Fill TCPServerAddr Struct
    TCPServerAddr.sin_family = AF_INET;
    TCPServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    TCPServerAddr.sin_port = htons(8888);

    //STEP-3 Create Socket
    if ((TCPServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET) {
        printf("SERVER: TCP Server: Create Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: TCP Server: Create Socket Success\n");

    //STEP-4 bind
    if (bind(TCPServerSocket, (SOCKADDR*)&TCPServerAddr, sizeof(TCPServerAddr)) == SOCKET_ERROR) {
        printf("SERVER: Binding Failed, Error: %d", WSAGetLastError());
    }
    printf("SERVER: Binding Success\n");

    //STEP-5 Listen
    if (listen(TCPServerSocket, 2) == SOCKET_ERROR) {
        printf("SERVER: Listen Failed, Error: %d", WSAGetLastError());
    }
    printf("SERVER: Listen Success: Listening for incoming connection...\n");

    // STEP-6 Accept
    if ((AcceptSocket = accept(TCPServerSocket, (SOCKADDR *)&TCPClientAddr, &TCPClientAddrSize)) == INVALID_SOCKET) {
        printf("SERVER: Accept Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: Connection Accepted\n");

    // STEP-7 Send Message to Client
    if (send(AcceptSocket,SendBuffer, sizeof(SendBuffer), 0) == SOCKET_ERROR) {
        printf("SERVER: Send Failed Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: Message Sent!\n");

    // STEP-8 Recv Message from Client
    if (recv(AcceptSocket,RecvBuffer, sizeof(RecvBuffer), 0) == SOCKET_ERROR) {
        printf("SERVER: Receive Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: Received Message From Client: %s\n", RecvBuffer);

    // STEP-9 Close Socket
    if (closesocket(TCPServerSocket) == SOCKET_ERROR) {
        printf("SERVER: Close Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: Close Socket Success\n");

    // STEP-10 Clean
    if (WSACleanup() == SOCKET_ERROR) {
        printf("SERVER: WSACleanup Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: WSACleanup Success\n");

    return 0;
}




/*
run:

TCP SERVER
SERVER: WSAStartUp Success
SERVER: TCP Server: Create Socket Success
SERVER: Binding Success
SERVER: Listen Success: Listening for incoming connection...
SERVER: Connection Accepted
SERVER: Message Sent!
SERVER: Received Message From Client: Hello from Client!
SERVER: Close Socket Success
SERVER: WSACleanup Success

*/
// TCP Client program

#include <stdio.h>
#include <WinSock2.h>

int main() {
    printf("TCP CLIENT\n");

    WSADATA WinSockData;

    SOCKET TCPClientSocket;
    struct sockaddr_in TCPServerAddr;
    char RecvBuffer[512];
    char SendBuffer[512] = "Hello from Client!";

    //STEP-1 WSASatrtUp
    if (WSAStartup(MAKEWORD(2,2),&WinSockData) != 0) {
        printf("CLIENT: WSAStartUp Failed\n");
    }
    printf("CLIENT: WSAStartUp Success\n");

    // STEP-2 Create Socket
    if ((TCPClientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET) {
        printf("CLIENT: TCP Client: Create Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: TCP Client: Create Socket Success\n");

    // STEP-3 Fill TCPServerAddr
    TCPServerAddr.sin_family = AF_INET;
    TCPServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    TCPServerAddr.sin_port = htons(8888);

    // STEP-4 Connect to Server
    if (connect(TCPClientSocket, (SOCKADDR *)&TCPServerAddr, sizeof(TCPServerAddr)) == SOCKET_ERROR) {
    //if (iConnect == SOCKET_ERROR) {
       printf("CLIENT: Connection Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: Connection Success\n");

    // STEP-5 Recv Message From Server
    if (recv(TCPClientSocket, RecvBuffer, sizeof(RecvBuffer), 0) == SOCKET_ERROR) {
        printf("CLIENT: Receive Message Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: Received Message From Server: %s\n", RecvBuffer);

    // STEP-6 Send Message to Server
    if (send(TCPClientSocket,SendBuffer,sizeof(SendBuffer),0) == SOCKET_ERROR) {
        printf("CLIENT: Send Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: Message Sent!\n");

    // STEP-7 Close Socket
    if (closesocket(TCPClientSocket) == SOCKET_ERROR) {
        printf("CLIENT: Close Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: Close Socket Success\n");

    // STEP-8 Clean
    if (WSACleanup() == SOCKET_ERROR){
        printf("CLIENT: WSACleanup Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: WSACleanup Success\n");

    return 0;
}




/*
run:

TCP CLIENT
CLIENT: WSAStartUp Success
CLIENT: TCP Client: Create Socket Success
CLIENT: Connection Success
CLIENT: Received Message From Server: Hello from Server!
CLIENT: Message Sent!
CLIENT: Close Socket Success
CLIENT: WSACleanup Success

*/

 




answered Apr 25 by avibootz
...