建立两台ubuntu虚拟机使用socket通讯的代码,客户端和服务端分开写,用c语言写
时间: 2023-08-31 16:27:35 浏览: 186
### 回答1:
以下是一个简单的示例,展示如何在两个Ubuntu虚拟机之间使用socket通信。其中一个虚拟机充当服务器,另一个充当客户端。请注意,此示例只供参考,您可能需要根据您的具体需求进行更改。
服务端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
perror("accept");
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
return 0;
}
```
客户端代码:
```
#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 <arpa/inet.h>
#define PORT 8080
int main(int argc, char const *argv[]) {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n",buffer );
return 0;
}
```
请注意,这些示例代码假定您的虚拟机都在同一网络中,并且其中一个虚拟机可以充当服务器。如果您需要跨网络进行通信或者需要使用多个服务器,则需要进行更改。
### 回答2:
建立两台Ubuntu虚拟机使用Socket通信的代码,客户端和服务端分开写,可以用C语言编写。以下是一个简单的例子供您参考:
### 服务端代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
int main() {
int serverSocket, newSocket;
struct sockaddr_in serverAddress, clientAddress;
int addrlen = sizeof(clientAddress);
char buffer[1024] = {0};
char *hello = "Hello from server";
// 创建套接字
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址结构
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);
// 绑定套接字
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听连接
if (listen(serverSocket, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
// 接受连接
if ((newSocket = accept(serverSocket, (struct sockaddr *)&clientAddress, (socklen_t*)&addrlen)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
// 读取客户端发送的消息
read(newSocket, buffer, 1024);
printf("Client: %s\n", buffer);
// 向客户端发送消息
send(newSocket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
```
### 客户端代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int main() {
int clientSocket;
struct sockaddr_in serverAddress;
char buffer[1024] = {0};
char *hello = "Hello from client";
// 创建套接字
if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址结构
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
// 将IPv4地址从点分十进制转换为二进制格式
if (inet_pton(AF_INET, "127.0.0.1", &serverAddress.sin_addr) <= 0) {
perror("inet_pton failed");
exit(EXIT_FAILURE);
}
// 连接服务器
if (connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}
// 向服务器发送消息
send(clientSocket, hello, strlen(hello), 0);
printf("Hello message sent\n");
// 读取服务器发送的消息
read(clientSocket, buffer, 1024);
printf("Server: %s\n", buffer);
return 0;
}
```
以上是一个简单的Socket通信示例。服务端代码创建了一个Server Socket并绑定到指定端口,然后监听来自客户端的连接。一旦有客户端连接成功,服务端通过read函数读取客户端发送的消息并打印,然后通过send函数向客户端发送一条消息。
客户端代码创建了一个Client Socket,并将其连接到指定的服务器地址和端口。连接成功后,客户端通过send函数向服务器发送一条消息,然后通过read函数读取服务器的响应消息并打印。
这两段代码可以分别在两台Ubuntu虚拟机上编译和运行,实现简单的Socket通信。注意,这只是一个基本的示例,实际项目中可能需要更多的错误处理和完善的功能。
### 回答3:
建立两台Ubuntu虚拟机使用Socket通讯的代码,客户端和服务端分开写,可以使用C语言编写。以下为示例代码:
服务端代码(server.c):
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *hello = "Hello from server";
// 创建socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置socket选项
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// 绑定socket到指定的端口
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听连接
if (listen(server_fd, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
// 接受客户端连接请求
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
// 接收和发送数据
valread = read(new_socket, buffer, BUFFER_SIZE);
printf("Client: %s\n", buffer);
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
```
客户端代码(client.c):
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[BUFFER_SIZE] = {0};
// 创建socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// 将IPv4地址从文本转换为二进制格式
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported\n");
return -1;
}
// 连接到服务器
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed\n");
return -1;
}
// 发送和接收数据
send(sock, hello, strlen(hello), 0);
printf("Hello message sent\n");
valread = read(sock, buffer, BUFFER_SIZE);
printf("Server: %s\n", buffer);
return 0;
}
```
这些代码使用基本的TCP/IP Socket编程技术,在本地主机上建立了简单的服务端和客户端之间的连接。服务端等待客户端连接请求,接收客户端发送的消息并返回一个“Hello”消息。客户端连接到服务端,发送一个“Hello”消息,并接收服务端返回的消息。
请注意,上述代码仅提供了基本的功能和示例。实际使用时,还需要进行错误处理、异常情况处理以及其他必要的安全性和可靠性增强。希望这个示例对您有所帮助。
阅读全文