Git   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了分段错误:11,在TCP客户端大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

内存分配或pipe理出错时,通常会出现分段错误。 但在这种情况下,我不知道什么是错的。 任何build议将有所帮助。 我正在尝试连接到一个SELEct()服务器。

#include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<neTinet/in.h> #include<String.h> #include<stdlib.h> #include<unistd.h> #include<netdb.h> #define INPUT "Socket TCP" #define BUF_SIZE 1024 int main(int argc,char *argv[]){ int sockt; struct sockaddr_in serv; struct hostent *host; char buff[BUF_SIZE]; serv.sin_family = AF_INET; /*host = argv[1]; host = malloc (1 + strlen (argv[1])); */ host = gethostbyname (argv[1]); printf("1"); if(host == 0) { perror("gethostbyname Failed"); close(sockt); exit(1); } else printf("gethost name succeeded n"); sockt = socket(AF_INET,SOCK_STREAM,0); printf("2"); if(sockt < 0) { perror("socket Failed"); exit(1); } else printf("socket connected n"); printf("3"); memcpy(&serv.sin_addr,host->h_addr,host->h_length); serv.sin_port = htons(1234); /*Convert from host byte order to network byte order*/ printf("4"); /*Condition to check if the client has connected*/ if(connect(sockt,(struct sockaddr *) &serv,sizeof(serv)) <0) { perror("Failed to connect"); close(sockt); exit(1); } else printf("5"); /*Condition to check if the data is sent*/ if(send(sockt,INPUT,sizeof(INPUT),0) < 0) { perror("Failed to send the data"); close(sockt); exit(1); } else printf("data sent"); printf("The data sent is %sn",INPUT); close(sockt); return 0; }

检测内存泄漏在C + + Qt结合?

无法访问位于Windows运行时应用程序的Assets文件夹中的Zip文件

Windows窗体+ Excel工作表

如何在Linux中使用Google Protobufs来确保相同的输出文件(* .o

你能解释这个C代码吗? (创build一个deamon程序)

正如WhozCraig所提到的,问题在于您没有提供主机名或IP地址。 你也没有检查提供的参数的数量

我运行你的程序,并没有发现任何段错误与正确的参数即host_name提供。

你应该包括以下检查:

if(argc<2) { printf("usage : %s hostname",argv[0]); exit(0); }

如果没有提供主机名,这将不允许程序继续。

以./<excecutable> <host_name>运行程序

首先尝试与本地主机。

即./tcp_client localhost

希望这可以帮助!

大佬总结

以上是大佬教程为你收集整理的分段错误:11,在TCP客户端全部内容,希望文章能够帮你解决分段错误:11,在TCP客户端所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: