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 client server communication on windows in C

1 Answer

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

#include <stdio.h>
#include <winsock2.h>
 
int main() {
	// Server Program

	//printf("From server: Listening for incoming connection...\n");
	//getchar();

	WSADATA WSAData;
	SOCKET serverSock, clientSock;
	SOCKADDR_IN serverAddr, clientAddr;
	
	WSAStartup(MAKEWORD(2, 0), &WSAData);
	 
	serverSock = socket(AF_INET, SOCK_STREAM, 0);
	
	serverAddr.sin_addr.s_addr = INADDR_ANY;
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(8888);
	
	bind(serverSock, (SOCKADDR *)&serverAddr, sizeof(serverAddr));
	
	listen(serverSock, 0);
	
	printf("From server: Listening for incoming connection...\n");

	char buf[512] = "";
	int clientAddrSize = sizeof(clientAddr);
	if ((clientSock = accept(serverSock, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET) {
		printf("From server: Client connected to server\n");
		recv(clientSock, buf, sizeof(buf), 0);
		printf("From server: Client message: %s\n", buf);
		
		closesocket(clientSock);
		printf("From server: Client disconnected from server\n");
	}

	getchar();
	
	return 0;
}
 
 
 
/*
run:
 
From server: Listening for incoming connection...
From server: Client connected to server
From server: Client message: E.T. Phone Home
From server: Client disconnected from server
 
*/
// Client Program

#include <stdio.h>
#include <winsock2.h>
 
int main() {
	// Client Program
	
    WSADATA WSAData;
	SOCKET serverSock;
	SOCKADDR_IN sockAddr;
	
	WSAStartup(MAKEWORD(2, 0), &WSAData);
	
	serverSock = socket(AF_INET, SOCK_STREAM, 0);
	
	sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	sockAddr.sin_family = AF_INET;
	sockAddr.sin_port = htons(8888);
	
	connect(serverSock, (SOCKADDR *)&sockAddr, sizeof(sockAddr));
	
	printf("From client: Connected to server\n");
	
	char buf[512] = {'E', '.', 'T', '.', ' ', 'P', 'h', 'o', 'n', 'e', ' ', 'H', 'o', 'm', 'e'};
	send(serverSock, buf, sizeof(buf), 0);
	printf("From client: Message sent\n");
		
	closesocket(serverSock);
	printf("From client: Socket closed\n");

	getchar();
	
	return 0;
}
 
 
 
/*
run:
 
From client: Connected to server
From client: Message sent
From client: Socket closed
 
*/

 




answered Apr 24 by avibootz
...