Write c++ code fore a server and a client where the client connects to the server and…
- User
Write c++ code fore a server and a client where the client connects to the server and writes a message and then closes the connec
Assistant
#include <iostream> #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 <arpa/inet.h> using namespace std; int main() { // create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { perror("socket"); exit(1); } // specify an address for the socket struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // connect to the server if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { perror("connect"); exit(1); } // send a message to the server char message[100]; cin.getline(message, 100); send(sockfd, message, strlen(message), 0); // close the socket close(sockfd); return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.