Notes
Notes - notes.io |
#include "CustomerManagerService.h"
#define LHOST NULL
#define LPORT "42424"
#define RECVBUFSIZE 58623
int __cdecl main()
{
WSADATA wsaData = { 0 };
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
printf("WSAStartup failed: %dn", result);
return -1;
}
struct addrinfo hints = { 0 };
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
struct addrinfo *ainfo;
result = getaddrinfo(LHOST, LPORT, &hints, &ainfo);
if (result != 0) {
printf("getaddrinfo failed: %dn", result);
WSACleanup();
return -1;
}
SOCKET listenSocket;
if ((listenSocket = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol)) == INVALID_SOCKET) {
printf("socket() failed with error: %ldn", WSAGetLastError());
freeaddrinfo(ainfo);
WSACleanup();
return -1;
}
// Setup the TCP listening socket
if ((bind(listenSocket, ainfo->ai_addr, (int)ainfo->ai_addrlen)) == SOCKET_ERROR) {
printf("bind() failed with error: %dn", WSAGetLastError());
freeaddrinfo(ainfo);
closesocket(listenSocket);
WSACleanup();
return -1;
}
freeaddrinfo(ainfo);
// Listen on the socket
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("listen() failed with error: %ldn", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return -1;
}
printf("-=-=-=-=-=-=- Listening for connections -=-=-=-=-=-=- n");
while (1) {
SOCKET clientSocket;
if ((clientSocket = accept(listenSocket, NULL, NULL)) == INVALID_SOCKET) {
printf("accept failed: %dn", WSAGetLastError());
continue;
}
printf("Connection received from remote host.n");
_beginthread(&handleConnection, 0, (void*)clientSocket);
printf("Connection handed off to handler thread.n");
}
}
void __cdecl handleConnection(void *param) {
SOCKET clientSocket = (SOCKET)param;
char recvbuf[RECVBUFSIZE] = { ' ' };
size_t recvbufUsed = 0;
const char* msgPleaseSendShorterLines = "Please send shorter lines.";
const char* msgBye = "Bye!";
while (1) {
size_t buf_remain = sizeof(recvbuf) - recvbufUsed;
if (buf_remain < 1) {
printf("[!] recvbuf exhausted. Giving up.n");
send(clientSocket, msgPleaseSendShorterLines, strlen(msgPleaseSendShorterLines), 0);
break;
}
int result = recv(clientSocket, (void*)&recvbuf[recvbufUsed], buf_remain, 0);
if (result == 0) {
printf("Client disconnected.n");
break;
}
else if (result < 0) {
printf("recv() failed: %d.n", WSAGetLastError());
break;
}
printf("Bytes received: %dn", result);
recvbufUsed += result;
char *line_start = recvbuf;
char *line_end;
while ((line_end = (char*)memchr((void*)line_start, 'n', recvbufUsed - (line_start - recvbuf))) != 0)
{
*line_end = ' ';
if (strcmp(line_start, "exit") == 0) {
printf("Client requested exit.n");
send(clientSocket, msgBye, strlen(msgBye), 0);
closesocket(clientSocket);
return;
}
doResponse(clientSocket, line_start);
line_start = line_end + 1;
}
recvbufUsed -= (line_start - recvbuf);
memmove_s(recvbuf, sizeof(recvbuf), line_start, recvbufUsed);
}
closesocket(clientSocket);
return;
}
int __cdecl doResponse(SOCKET clientSocket, char *clientName) {
char response[128];
sprintf(response, "ERROR %s...n", clientName);
int result = send(clientSocket, response, strlen(response), 0);
if (result == SOCKET_ERROR) {
printf("send failed: %dn", WSAGetLastError());
closesocket(clientSocket);
return -1;
}
printf("Bytes sent: %dn", result);
return 0;
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team