以C语言写一、编写服务器和客户端: 服务器:1.socket() 创建流式套接字. 2.bind() 绑定自己的地址信息. 3.listen()设置成被动监听状态. 4.accept() 等待并接受连接请求. 5.recv()/recvfrom() 接受数据信息. read() 6.send()/sendto() 发送数据信息. write() 7.close() 关闭套接字. 客户端:1.socket() 创建流式套接字. (2.bind() 绑定自己的地址信息.) 3.connet() 向服务器端的套接字发送连接请求. 4.send()/sendto() 发送数据信息. write() 5.recv()/recvfrom() 接受数据信息. read() 6.close() 关闭套接字.
时间: 2023-12-09 21:06:25 浏览: 94
ChatappClientServer:使用C进行套接字编程,创建TCP客户端和TCP服务器
以下是一个简单的服务器和客户端的示例代码,使用了C语言的套接字编程:
服务器端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main(int argc, char const *argv[]) {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[MAX_BUFFER_SIZE] = {0};
char *hello = "Hello from server";
// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attach 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);
// Bind the socket to the specified address and port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Start listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// Wait for incoming connections and handle them
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket, buffer, MAX_BUFFER_SIZE);
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/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main(int argc, char const *argv[]) {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[MAX_BUFFER_SIZE] = {0};
// Create socket file descriptor
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
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;
}
// Connect the socket to the specified address and port
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, MAX_BUFFER_SIZE);
printf("%s\n", buffer);
return 0;
}
```
这个示例代码实现了一个简单的回声服务器和客户端,当客户端连接到服务器后,发送一个“Hello from client”字符串,服务器接收到该字符串后,会将其回传给客户端,并在客户端上显示出来。如果需要实现其他功能,可以根据需要修改代码。
阅读全文