开发者社区> 问答> 正文

为什么老是报错呢?有什么问题???感谢回答?报错

#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>    // for sockaddr_in
#include <sys/types.h>    // for socket
#include <sys/socket.h>    // for socket
#include <stdio.h>        // for printf
#include <stdlib.h>        // for exit
#include <string.h>        // for bzero

 
#define LISTEN_QUEUE 20
#define SERVICE_PORT 1234

int service_sockfd;
int ret;
int sc;
int ret_cv;
static char buf[]="";
 FILE *stream;
int main(int argc,char *argv[])

{
  struct sockaddr_in service_addr;
	service_addr.sin_family =AF_INET;
	service_addr.sin_port =htons(SERVICE_PORT);
	service_addr.sin_addr.s_addr =htonl(INADDR_ANY);
	//bzero(&(service_addr),sizeof(service_addr));
	
	service_sockfd =socket(AF_INET,SOCK_STREAM,0);
	if(-1 == service_sockfd)
	{
	 perror("sosck failed\n");
	 exit(-1);
	}

	if(bind(service_sockfd,(struct sockaddr*)&service_addr,sizeof(service_addr))==-1)
	{
	 printf("bind failed\n");
	 exit(-1);
	}
	if( -1 ==listen(service_sockfd,10))
	{
	 printf("listen: %s\n",hstrerror(h_errno));
	 exit(-1);
	}
	printf("listening...................\n");
	while(1)
{
	
	 socklen_t leng = sizeof(service_addr);
	 struct sockaddr_in client_addr;
	 /*
	 sc =accept(service_sockfd,(struct sockaddr*)&client_addr,&(leng));
	 if(-1 == sc)
	 {
	  printf("accept failed\n");
	  break;
	 }
	 else
	 {
	  printf("Service IP:%s::Service Port%d\n",inet_ntoa(client_addr.sin_addr),client_addr.sin_port);
	 }
	 */
	 sc = accept(service_sockfd,(struct sockaddr*)&client_addr,&(leng));
	 if(sc == -1)
	  {
	  printf("accept failed\n");
	  break;
	 }
	 else
	 {
	  printf("Service IP:%s::Service Port%d\n",inet_ntoa(client_addr.sin_addr),client_addr.sin_port);
	 }
	
	  printf("sc =[%d]\n",sc);
	 strcpy(buf,"hello 客户端!!");
	 if((ret =send(sc,buf,strlen(buf)+1,0))==-1)
	 {
	   perror("send failed\n");
	   
	 }	
	  printf("ret =[%d]\n",ret);
	 printf("the service sending!!!\n");
	 ret_cv =recv(sc,buf,1024,0);
	 if(-1 ==ret_cv)
	 {
	   perror("recv failed\n");
		 break;
	 }
	 printf("service ::recv: - %s\n",buf);
	/*
	if(fopen("file1","w+")==NULL)
	 {
	  printf("the file open error\n");
	 	  break;
	 }
	else
	{
		 printf("the file open successful\n");
	}
	 
	 int lengsize = 0;
	

	    printf("lengsize = %d\n",lengsize);
	  
	      if(recv(sc,buf,1024,0)<0)
	      {
			    printf("Send File is Failed\n");
			    break;
	      }
	   printf("Service recv:: %s\n",buf);
	    
      
	  //}
 
   if(fclose(stream))
   {
   printf("The file 'data' was not closed! \n");
   exit(1);  
	 } 
	 */
	 close(sc);     
} 
	close(service_sockfd);
	return 0;
	
}



展开
收起
爱吃鱼的程序员 2020-06-14 21:47:43 464 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    问怎么没把错误贴出来呢?<imgsrc="http://www.oschina.net/js/ke/plugins/emoticons/images/10.gif"alt=""/><imgsrc="http://static.oschina.net/uploads/space/2014/0915/180807_oI7V_1581898.png"alt=""/>

    static<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:15.397727012634277px;background-color:#FFFFFF;">char<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:15.397727012634277px;background-color:#FFFFFF;">buf[]="";

    这行有问题,估计你接受数据的时候,把一些变量给覆盖了。

    charbuf[]="";
    这行,buf的长度为1,接收的时候就会内存越界,就出错。
    你要么用charbuf[1024]="";或者用malloc动态分配内存。

    奥,说的也是的,但是还是有问题,不知道在那里的???

    2020-06-14 21:48:00
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
MaxCompute Logview参数详解和问题排查(废弃) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载