- Blog entry posted in 'Uncategorised', October 26, 2011.
Here is code for a simple socket server. I built and tested it using Microsoft Visual C++ 2005. You can build the source file and run it under a command window, connect using telnet either from a remote machine or local host. Using the local host is handy because you can test it using only one machine. To build the code:
Open a console C++ project under Visual Studio
Visual Studio inserts a template C++ file with the same name as your project. Open the template file
Copy the server code and paste it into the template file, under #include "stdafx.h" line. Delete that line in the server code
right click on your project name entry in the explorer window of Visual Studio and chose "properties"
expand the "linker" catagory by clicking the "+" sign
Choose "Input"
in the space next to "Additional Dependencies, type ws2_32.lib
click "Apply" then "OK"
Select Build->Build Solution from the tools menu
Verify the build succeded with no errors.
Now you can test the code. Open a command shell and navigate to your project folder. Navigate further to the debug folder if you accepted the default configuration and build the debug version.
Type the name of the executable file, same name as the project.
The server is now running. You will see "Tcp Server Application for use with telnetcli and local host" on your command shell.
Now open another command shell and type "telnet 127.0.0.1 40"
The telnet client is running, you will see "You are connected to the text server at 127.0.0.1"
type something in the telnet application, hit enter. You'll see your text show up in the server window. (there is some other information in the server window)
Next time I'll go over the code and what it means. Meanwhile, here is the code:
// tcpserver.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "stdafx.h"
#include "winsock.h"
#define SERV_PORT 40
#define WS_VERSION_REQD 0x0101
int _tmain(int argc, _TCHAR* argv)
{
int listenfd, connfd;
int rx_code = 0;
struct sockaddr_in cliaddr, servaddr;
char rdata;
char tdata;
char *prdata = rdata;
int clilen;
int iError = 0;
WSADATA wsaData;
printf("Tcp Server Application for use with telnetcli and local host\n");
iError = WSAStartup(WS_VERSION_REQD,&wsaData);
if (iError != 0) {
printf ("\nWINSOCK.DLL does not respond\n");
// exit(0);
}
listenfd = socket(AF_INET, SOCK_STREAM, 0);
//bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family =AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd, (sockaddr *)&servaddr, sizeof(servaddr));
listen(listenfd, 5);
printf("Listening on port %d\n", SERV_PORT);
clilen=sizeof(cliaddr);
printf("%d\n", clilen);
connfd=accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
printf("Accepted And Connection from %s\n", inet_ntoa(cliaddr.sin_addr));
sprintf(tdata,"You are connected to the Text Server at %s\r\n", inet_ntoa(cliaddr.sin_addr));
send(connfd, tdata, strlen(tdata), 0);
while(1) {
while(!strchr(rdata, '\n')) {
rx_code = recv(connfd, prdata, sizeof(rdata), 0);
prdata +=rx_code;
*prdata = '\0';
}
// if(strstr(rdata, "End Of Transmission"))
// printf("%s", rdata);
printf("%s\n", rdata);
memset(rdata, 0, prdata-rdata+1);
prdata=rdata;
}
return 0;
}