Created
October 28, 2013 16:12
-
-
Save aguegu/7199774 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================ | |
// Name : socket_demo.cpp | |
// Author : aGuegu | |
// Version : | |
// Copyright : GPL v2.0 | |
// Description : Hello World in C++, Ansi-style | |
//============================================================================ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
void error(const char *msg) { | |
perror(msg); | |
exit(0); | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 3) { | |
fprintf(stderr, "usage %s hostname port\n", argv[0]); | |
exit(0); | |
} | |
int port_number = atoi(argv[2]); | |
int socket_fd = socket(AF_INET, SOCK_STREAM, 0); | |
if (socket_fd < 0) | |
error("ERROR opening socket"); | |
struct hostent * server = gethostbyname(argv[1]); | |
if (server == NULL) { | |
fprintf(stderr, "ERROR, no such host\n"); | |
exit(0); | |
} | |
struct sockaddr_in server_address; | |
bzero((char *) &server_address, sizeof(server_address)); | |
server_address.sin_family = AF_INET; | |
bcopy((char *) server->h_addr, (char *)&server_address.sin_addr.s_addr, server->h_length); | |
server_address.sin_port = htons(port_number); | |
if (connect(socket_fd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) | |
error("ERROR connecting"); | |
printf("Please enter the message: "); | |
char buffer[256]; | |
bzero(buffer, 256); | |
fgets(buffer, 255, stdin); | |
int n = write(socket_fd, buffer, strlen(buffer)); | |
if (n < 0) | |
error("ERROR writing to socket"); | |
bzero(buffer, 256); | |
n = read(socket_fd, buffer, 255); | |
if (n < 0) | |
error("ERROR reading from socket"); | |
printf("%s\n", buffer); | |
close(socket_fd); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================ | |
// Name : socket_server_demo.cpp | |
// Author : aGuegu | |
// Version : | |
// Copyright : GPL v2.0 | |
// Description : Hello World in C++, Ansi-style | |
//============================================================================ | |
/* A simple server in the internet domain using TCP | |
The port number is passed as an argument */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
void error(const char *msg) { | |
perror(msg); | |
exit(1); | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
fprintf(stderr, "ERROR, no port provided\n"); | |
exit(1); | |
} | |
int port_number = atoi(argv[1]); | |
int socket_fd = socket(AF_INET, SOCK_STREAM, 0); | |
if (socket_fd < 0) | |
error("ERROR opening socket"); | |
struct sockaddr_in server_address; | |
bzero((char *) &server_address, sizeof(server_address)); | |
server_address.sin_family = AF_INET; | |
server_address.sin_addr.s_addr = INADDR_ANY; | |
server_address.sin_port = htons(port_number); | |
if (bind(socket_fd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) | |
error("ERROR on binding"); | |
listen(socket_fd, 5); | |
struct sockaddr_in client_address; | |
socklen_t client_length = sizeof(client_address); | |
int socket_fd_sub = accept(socket_fd, (struct sockaddr *) &client_address, &client_length); | |
if (socket_fd_sub < 0) | |
error("ERROR on accept"); | |
char buffer[256]; | |
bzero(buffer, 256); | |
int n = read(socket_fd_sub, buffer, 255); | |
if (n < 0) | |
error("ERROR reading from socket"); | |
printf("Here is the message: %s\n", buffer); | |
n = write(socket_fd_sub, "I got your message", 18); | |
if (n < 0) | |
error("ERROR writing to socket"); | |
close(socket_fd_sub); | |
close(socket_fd); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment