C++ socket code
时间: 2023-10-16 15:07:30 浏览: 102
Sorry, as an AI language model, I cannot provide you with a complete code without knowing the specific requirements of your project. However, I can give you a basic example of a client-server communication code using sockets in C.
Server Side:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(){
int server_socket, client_socket, port_number;
socklen_t client_length;
char buffer[256];
struct sockaddr_in server_address, client_address;
// create socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("Error opening socket");
exit(1);
}
// initialize server address
bzero((char*) &server_address, sizeof(server_address));
port_number = 8080;
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(port_number);
// bind socket to address
if (bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address)) < 0) {
perror("Error binding socket");
exit(1);
}
// listen for incoming connections
listen(server_socket, 5);
// accept client connection
client_length = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*) &client_address, &client_length);
if (client_socket < 0) {
perror("Error accepting connection");
exit(1);
}
// read message from client
bzero(buffer, 256);
read(client_socket, buffer, 255);
printf("Received message from client: %s\n", buffer);
// send response to client
char response[] = "Hello from server!";
write(client_socket, response, strlen(response));
// close sockets
close(client_socket);
close(server_socket);
return 0;
}
```
Client Side:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main(){
int client_socket, port_number, n;
struct sockaddr_in server_address;
struct hostent* server;
char buffer[256];
// create socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("Error opening socket");
exit(1);
}
// get server by hostname
server = gethostbyname("localhost");
if (server == NULL) {
perror("Error finding server");
exit(1);
}
// initialize server address
bzero((char*) &server_address, sizeof(server_address));
port_number = 8080;
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);
// connect to server
if (connect(client_socket, (struct sockaddr*) &server_address, sizeof(server_address)) < 0) {
perror("Error connecting to server");
exit(1);
}
// send message to server
char message[] = "Hello from client!";
write(client_socket, message, strlen(message));
// read response from server
bzero(buffer, 256);
read(client_socket, buffer, 255);
printf("Received message from server: %s\n", buffer);
// close socket
close(client_socket);
return 0;
}
```
阅读全文