我想使用C在客户端和服务器之间建立两台PC之间的连接 我已经能够从客户端发送数据并通过服务器接收。 但是当服务器想要发送数据时,错误会出现在服务器端: 分段失败: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;
}
任何消化?
答案 0 :(得分:0)
执行调试构建(选项-g)然后调用gdb。它会打开您使用
的提示file "a.out"
加载可执行文件,然后调用“run”以在调试器的控制下实际运行程序。当segfault发生时,调用“backtrace”,它应该告诉你函数,也许它会发生在哪里。
但是当我看到代码时,我的第一个猜测是输入字符数组。发送时输入超过29个字符吗? ;)