Can somebody give me an example for C++ Sockets
This is a discussion on Example To C++ Sockets within the C++ Programming forums, part of the General Programming Boards category; Can somebody give me an example for C++ Sockets...
Can somebody give me an example for C++ Sockets
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, ¤t, 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); } }
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)