【发布时间】:2019-10-27 22:05:37
【问题描述】:
我在 localhost 上启动了一个客户端-服务器程序。 我的程序应该将本地目录的文件列表从服务器发送到客户端,客户端应该打印它。客户端随机打印一些文件名(不是全部)或根本不打印。
如果我为客户端(nc localhost 21)或服务器(sudo nc -l 21)更改 nc,一切正常。
客户:
#ifndef DEBUG
#define DEBUG
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#ifndef SIZE
#define SIZE 1000
#endif
#define SERVER_PORT 21
#define CLIENT_IP 0
#define SERVER_IP 0
void recv_smth2(int skt);
int connect2(int IP, int PORT);
int main(){
int port = htons(SERVER_PORT);
int sck = connect2(SERVER_IP,port);
if (sck == -1){
return 0;
}
recv_smth2(sck);
}
void recv_smth2(int skt){
char BUF[SIZE];
while(1){
int l = recv(skt,BUF,SIZE-1,0);
if(l == -1){
perror("recv error");
}
BUF[l] = 0;
printf("%s", BUF);
}
}
int connect2(int IP, int PORT){
//^ in network byte order^
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = PORT;
addr.sin_addr.s_addr = IP;
int skt = socket( PF_INET, SOCK_STREAM, 0 );
if (skt == -1){
perror("socket error");
}
int cnct = connect( skt, (struct sockaddr*) &addr, sizeof(addr) );
if (cnct == -1){
perror("cnct error");
return -1;
}
return skt;
}
服务器:
#define SERVER_PORT 21
#ifndef SIZE
#define SIZE 1000
#endif
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<dirent.h>
#include<sys/stat.h>
int open_port(int PORT);
void send_string(const char *s,int n,int sk);
void send_file_list(int skt);
int main(){
int port = htons(SERVER_PORT);
int sk = open_port(port);
if (sk == -1){
perror("can't open port");
}
send_string("Hello!\n", sizeof("Hello!\n"), sk);
send_file_list(sk);
}
void send_file_list(int skt){
char buf[SIZE];
DIR *dir;
struct dirent *ent;
if((dir = opendir(".")) != NULL ){
while ((ent = readdir(dir)) != NULL){
int n = strnlen(ent->d_name, SIZE - 2);//without \0
snprintf(buf,n+2,"%s\n",ent->d_name);//\n\0
send_string(buf,n+2,skt);
}
}
}
void send_string(const char *s, int n, int sk){
send(sk,s,n,0);
}
// all in network byte order
int open_port(int PORT){
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
//addr.sin_port = htons(PORT);
addr.sin_port = PORT;
int skt = socket(PF_INET, SOCK_STREAM, 0);
int enable = 1;
if(setsockopt(skt,SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)))
perror("setsockopt(SO_REUSEADDR)failed");
bind(skt, (struct sockaddr*) &addr, sizeof(addr));
if(listen(skt, 0) == -1){
perror("listen ");
return -1;
}
struct sockaddr_storage addr_s;
memset(&addr_s, 0, sizeof(addr_s));
int addr_size = sizeof(addr_s);
int fd = accept( skt, (struct sockaddr*) &addr_s, &addr_size);
if (fd == -1){
perror("accept");
}
if (fd != 0){
}
return fd;
}
编译
gcc client.c -o client
gcc server.c -o server
run:
$ sudo ./server
$ client
客户端应打印“Hello”,然后列出服务器目录中的所有文件。 它只打印“Hello”,然后从服务器目录中随机打印一些文件名。 如果我使用 nc 而不是 client,我每次都会得到每个文件的名称。
【问题讨论】:
-
也许我遗漏了一些明显的东西,但是......既然你使用的是 IPv4,你什么时候不只是在服务器上使用
struct sockaddr_in而不是struct sockaddr_storage?
标签: c sockets network-programming client-server