Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Samples

Simple Winsock Message Server

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Simple Winsock Message Server

This small sample console application acts as a simple message server, if you connect to port 1111 with telnet you will receive the message "Hello World!" and then be disconnected. Very simple, useful to play around with.

program WIN32CUI 'Sample';

#include 'ziron32.zir';
#include 'smm32.zir';

#include 'kernel32.zir';
#include 'user32.zir';
#include 'messages.zir';

#include 'wsock32.zir';
#include 'console.zir';


const DEFAULT_PORT = 1111;

WSADATA ws;
WSOCKET listensocket;
WSOCKET clientsocket;
sockaddr_in addrinfo;

EAX = WSAStartup(WSOCKVER2_2, @ws);
listensocket = socket(PF_INET, SOCK_STREAM, 0);
addrinfo.sin_family = AF_INET;
addrinfo.sin_addr = INADDR_ANY;
addrinfo.sin_port = htons(DEFAULT_PORT);
bind(listensocket, @addrinfo, sizeof sockaddr_in);
    

listen(listensocket, 20);
while () {
  clientsocket = accept(listensocket, nil, nil);  
  send(clientsocket, 'Hello World!', 12, 0);
  closesocket(clientsocket);
}

//console will never get here. but just in case.
ExitProcess(0);