Thread: Example To C++ Sockets

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Example To C++ Sockets

    Can somebody give me an example for C++ Sockets

  2. #2

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Here's something I wrote for a class a while back:


    Client:
    Code:
    #include "userPanel.h"
    
    int main (int argc, const char * argv[]) {
    	if (argc < 3)
    	{
    		printf("Usage: userPanel [server] [port]\n--using: ");
    		if (argc == 1)
    		{
    			printf("localhost 65530\n");
    			ip = malloc(15*sizeof(char));
    			ip = "127.0.0.1";
    			port = (unsigned short) 65530;
    		}
    		if (argc == 2)
    		{
    			printf("%s 65530\n", argv[1]);
    			ip = malloc(15*sizeof(char));
    			strncpy(ip, argv[1], sizeof(ip));
    			port = (unsigned short) 65530;
    		}
    	}
    	else
    	{
    		port = (unsigned short) atoi(argv[2]);
    		ip = malloc(15*sizeof(char));
    		strncpy(ip, argv[1], sizeof(ip));
    	}
    	//server
    	struct sockaddr_in calcserver;
    	memset(&calcserver, 0, sizeof(calcserver));
    	calcserver.sin_family = AF_INET;
    	calcserver.sin_port = htons(port);
    	calcserver.sin_addr.s_addr = inet_addr(ip);
    	if(ip != NULL)
    	{
    		free(ip);
    		ip = NULL;
    	}
    	for(;;)
    	{
    		//send to server
    		printf("Calculation [float] [operation] [float]:");
    		struct equation thisEquation;
    		scanf("%f %s %f", &thisEquation.first, &thisEquation.operation, &thisEquation.second);
    		printf("\n%f %s %f\n", thisEquation.first, thisEquation.operation, thisEquation.second);
    		outsock = socket(AF_INET, SOCK_STREAM, 0);
    		if (outsock == -1)
    		{
    			printf("Error creating socket\n");
    			exit(-1);
    		}
    		if (connect(outsock, (struct sockaddr *)&calcserver, sizeof(calcserver)) < 0)
    		{
    			printf("Error Connecting to server\n");
    			exit(-1);
    		}
    		send(outsock, &thisEquation, sizeof(thisEquation), 0);
    		//recv from server
    		listen(outsock, 1);
    		recv(outsock, &result, sizeof(result), 0);
    		printf("Solution: %f\n", result);
    		close(outsock);
    	}
    	return 0;
    }

    Server:
    Code:
    #include "calculator.h"
    
    int main (int argc, const char * argv[]) {
    	if (argc < 2)
    	{
    		printf("Usage: calculator [port]\n--using: 65530\n");
    		port = 65530;
    	}
    	else
    		port = (unsigned short) atoi(argv[1]);
    	
    	memset(&serverAddr, 0, sizeof(serverAddr));
    	memset(&clientAddr, 0, sizeof(clientAddr));
    	serverAddr.sin_family = AF_INET;
    	serverAddr.sin_port = htons(port);
    	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    	serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    	//Bind
    	if (bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
    	{
    		printf("Error: bind()\nError Number:%d\n", errno);
    		exit(-1);
    	}
    	if (listen(serverSocket, 5) < 0)
    	{
    		printf("Error: listen()\nError Number:%d\n", errno);
    		exit(-1);
    	}
    	printf("Server ready!\n");
    	for (;;)
    	{
    		unsigned int length = sizeof(clientAddr);
    		clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &length);
    		struct equation current;
    		int bR = 0;
    		bR = recv(clientSocket, &current, sizeof(current), 0);
    		if(bR == -1)
    			printf("\n%s\n", strerror(errno));
    		printf("Client: %s\n", inet_ntoa(clientAddr.sin_addr));
    		if (strcmp(current.operation, "plus") == 0)
    			result = current.first + current.second;
    		if (strcmp(current.operation, "minus") == 0)
    			result = current.first - current.second;
    		if (strcmp(current.operation, "mult") == 0)
    			result = current.first * current.second;
    		if (strcmp(current.operation, "div") == 0)
    			result = current.first / current.second;
    		if (strcmp(current.operation, "expon") == 0)
    			result = pow(current.first, current.second);
    		if (strcmp(current.operation, "log") == 0)
    			result = log10(current.first) / log10(current.second);
    		if (strcmp(current.operation, "sqrt") == 0)
    			result = sqrt(current.first);
    		printf("%f %s %f = %f\n", current.first, current.operation, current.second, result);
    		send(clientSocket, &result, sizeof(result), 0);
    		close(clientSocket);
    	}
    }

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Here's a tutorial:

    Linux Howtos: C/C++ -> Sockets Tutorial

    It was the first result on Google for "C sockets"

    (Note: C and C++ sockets are essentially the same)
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. program structure with select sockets
    By n3v in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-03-2006, 06:34 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM