//PC间的通信测试recv与send函数,服务器端
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include<unistd.h>
#include <iostream>
#include<stdio.h>
#include <errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<utility>
#include<string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
using namespace std;
#define BUFF_LEN 1024
#define NUM_BUFF_LEN 10
const int port = 9000;
const int buffer =10;
int GetNum(int *sock);
void OnceText(int *sock);
void HandleText(int *sock,int num);
int WriteFile(char *buff,int num);
int main()
{
struct sockaddr_in client_addr;
struct sockaddr_in server_addr;
int client_addr_size = sizeof(struct sockaddr_in);
int server_sock,client_sock;
server_sock = socket(AF_INET,SOCK_STREAM,0);
if(-1==server_sock)
{
perror("socket");
return -1;
}
printf("server_sock created\n");
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(port);
if(-1 == bind(server_sock,(struct sockaddr *)&server_addr,sizeof(server_addr)))
{
perror("bind");
return -1;
}
printf("bind finished\n");
if(-1 == listen(server_sock,3))
{
perror("listen");
return -1;
}
printf("listening\n");
socklen_t client_addr_len = sizeof(client_addr);
char *rec_buff = new char[BUFF_LEN];
int rec_len=0;
int send_len;
const char *send_buffer = "send over";
int num;
while(1)
{
/*client_sock = accept(se
socket编程2——recv与send的一点理解测试
最新推荐文章于 2024-04-09 20:31:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)