All pastes #427398 Raw Edit

Something

public text v1 · immutable
·published 2007-04-06 19:27 UTC
rendered paste body
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;

#define MAXDATA 512
#define PORT 8000

int main() {
	// Vars
	int sIncoming,sSocket,sInfo;
	socklen_t sSize;
	char sBuffer[MAXDATA];
	
	// Structs
	struct sockaddr_in local;
	struct sockaddr_in incoming;
	
	// Program Starts
	
	local.sin_family=AF_INET;
	local.sin_addr.s_addr=INADDR_ANY;
	local.sin_port=htons(PORT);
	memset(&(local.sin_zero), '\0', 8);
	sSocket = socket(AF_INET,SOCK_STREAM,0);
	
	if (sSocket == -1) {
		cout << "Couldnt startup socket!" << endl;
		exit(0);
	}
	else {
		cout << "Socket Started!." << endl;
	}
	
	if(bind(sSocket,(sockaddr*)&local,sizeof(local))!= 0) {
		cout << "Couldnt startup binding!" << endl;
		exit(0);
	}
	else {
		cout << "Binding started!." << endl;
	}
	
	if(listen(sSocket,2) != 0) {
		cout << "Couldnt startup listening connections!" << endl;
		exit(0);
	}
	else {
		cout << "Listening Started!." << endl;
	}
	
	cout << "Listening at port " <<  PORT << ". PID: " << getpid() << endl;
	
	while (1) {
		sSize = sizeof(struct sockaddr_in);
		sIncoming = accept(sSocket, (struct sockaddr *)&incoming,&sSize);
		sInfo = recv(sSocket,sBuffer,MAXDATA-1,0);
		if (sInfo != -1) {
			// send(sSocket,"INFO",strlen("INFO"),0);
			cout << "Incoming: " << sBuffer << endl;
			send(sSocket,"INFO\n",strlen("INFO\n"),0);
		}
		else {
			perror("recv");
		}
	}
	close(sSocket);
	return 0;
}