0

我想使用客户端和使用 C 的服务器建立两台电脑之间的连接我已经能够从客户端发送数据并由服务器接收。但是当服务器要发送数据时,服务器端会出现错误:分段失败:11

这是我的程序:在客户端:

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <netdb.h>
#define MYPORT 4950
#define MAXBUFLEN 100 

int main() {
    char no[16], dt[30];
    printf("‐‐‐‐‐ PROGRAM CHATTING ‐‐‐‐‐\n");
    printf("To : ");
    scanf("%s", no);
    while(1){
        printf("Me : ");
        scanf("%s", dt); send(no, dt); receive();
   } 
} 

int send(char no[], char dt[])
{
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr; 
struct hostent *he;

int addr_len, numbytes;
if((he = gethostbyname(no)) == NULL) 
{
    perror("gethostbyname");
    exit(1); 
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{ //40
    perror("socket");
    exit(1); 
}
their_addr.sin_family = AF_INET;

their_addr.sin_port = htons(MYPORT); 
their_addr.sin_addr=*((struct in_addr*)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);
if((numbytes=sendto(sockfd,dt,strlen(dt),0,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)))==-1)
    {
    perror("sendto");
    exit(1); 
}
close(sockfd);
return 0; 
}

int receive()
{
     int sockfd;

struct sockaddr_in my_addr; 
struct sockaddr_in their_addr; 
struct hostent *he;

int addr_len, numbytes;
char buf[MAXBUFLEN];
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) //68
{
    perror("socket");
    exit(1); 
}
my_addr.sin_family = AF_INET; 
my_addr.sin_port = htons(MYPORT); 
my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero),'\0',8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
    perror("bind");
    exit(1); 
}
addr_len = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd,buf,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,&addr_len))==-1)
{
    perror("recvfrom");
    exit(1); }
buf[numbytes]='\0';

printf("%s : \"%s\"\n", inet_ntoa(their_addr.sin_addr), buf);
close(sockfd);
return 0; 
}

和服务器:

 #include <stdio.h> 
 #include <stdlib.h> 
 #include <unistd.h> 
 #include <errno.h> 
 #include <string.h> 
 #include <sys/types.h> 
 #include <sys/socket.h> 
 #include <netinet/in.h> 
 #include <arpa/inet.h> 
 #include <netdb.h>
 #define MYPORT 4950
 #define MAXBUFLEN 100
char noip[50]; 
int main() 
{
char no[16];
char dt[30];
printf("‐‐‐‐‐ PROGRAM CHATTING ‐‐‐‐‐\n");
receive();
strcpy (no, noip); 
while(1){
    printf("Me : ");
    scanf("%s", dt); 
    send( dt); 
    receive();
    }
 } 

 int send(char dt[30]) {
   int sockfd;
   struct sockaddr_in my_addr; 
   struct sockaddr_in their_addr; 
   struct hostent *he;
   int addr_len, numbytes;

if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
        perror("socket");
        exit(1); 
    }
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(MYPORT); 
their_addr.sin_addr=*((struct in_addr*)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);
if((numbytes=sendto(sockfd,dt,strlen(dt),0,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)))==-1)        
        {
        perror("sendto");
        exit(1); }
    close(sockfd);
    return 0; }

int receive() {
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr; 
struct hostent *he;
int addr_len, numbytes;
char buf[MAXBUFLEN];
char no[16];

if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
        perror("socket");
        exit(1); }

my_addr.sin_family = AF_INET; 
my_addr.sin_port = htons(MYPORT); 
my_addr.sin_addr.s_addr = INADDR_ANY; 
memset(&(my_addr.sin_zero),'\0',8);

if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
        perror("bind");
        exit(1); }

addr_len = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd,buf,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,&addr_len))==-1)
{
    perror("recvfrom");
    exit(1);}

buf[numbytes]='\0';
printf("%s : \"%s\"\n", inet_ntoa(their_addr.sin_addr), buf);
strcpy(no,inet_ntoa(their_addr.sin_addr));//copy data ke varibel no 
        close(sockfd);
        return 0;
}

有什么建议吗?

4

1 回答 1

0

进行调试构建(选项 -g),然后调用 gdb。它会在您使用的地方打开一个提示

file "a.out"

加载可执行文件,然后调用“运行”以在调试器的控制下实际运行您的程序。当段错误发生时,调用“回溯”,它应该告诉你函数,也许是它发生的行。

但是当我看到代码时,我的第一个猜测是输入 char 数组。发送时输入的字符是否超过 29 个?;)

于 2012-12-08T03:22:41.437 回答