开发者社区> 问答> 正文

【求助】SOCKET编程,网络编程

本人自学SOCKET,跟着教程写了几行代码 但是接收(recv)那个循环里面总是出问题,具体情况是运行到第二次的时候程序就蹦了。
我的改进:后来经过一天的思考推敲我发现是有一中返回情况SOCKET_ERRO没有处理
于是家进入了 但还是错误,所以特来贵吧寻求帮助,(ps:我之前试过不用realloc扩大空间储存程序是ok的,但是用了realloc就出问题了,但是我觉得主要问题又不是在realloc哪里,可能是recv那里),求大神花上几分钟看看我的代码,谢谢,请赐教!!!
代码

#include 
#include 
#pragma comment(lib,"ws2_32.lib")
int main(void)
{ 
WSADATA wd;
SOCKET c;
char recvBuf[1000]="", sendBuf[1000]="";
SOCKADDR_IN saddr;
/*1.初始化操作*/
if(WSAStartup(MAKEWORD(2,2),&wd)) //0表示调用成功
{
printf("调用失败\n");
return 0;
}
if(HIBYTE(wd.wVersion)!=2 || LOBYTE(wd.wVersion)!=2) //版本打开确认
{
printf("初始化失败");
WSACleanup();
return 1;
}
/*2.创建客户端socket*/
c = socket(AF_INET, SOCK_STREAM, 0);
if(c == INVALID_SOCKET)
{
printf("创建socket失败\n\n");
return 0;
}
/*3.定义要连接的服务端信息*/
char ip[200] = "";
memset(ip,0,sizeof(ip));
printf("请输入你要获取的ip:\n");
gets(ip);
saddr.sin_addr.S_un.S_addr = inet_addr(ip);
saddr.sin_family = AF_INET;
//char port;
// printf("请输入要获取的IP的端口:\n");
//scanf("%d",&port);
saddr.sin_port = htons(80);
/*4.连接服务端*/
if(connect(c, (SOCKADDR*)&saddr, sizeof(SOCKADDR)))
{
printf("链接失败\n");
return 0;
}
char address[200] = "";
memset(address,0,sizeof(address));
printf("请输入要连接的网址:\n");
gets(address);
strcpy(sendBuf,"GET / HTTP/1.0\r\n");
strcat(sendBuf,"host:");
strcat(sendBuf,address);
strcat(sendBuf,"\r\n\r\n");
send(c, sendBuf, strlen(sendBuf)+1, 0); 
memset(recvBuf,0,sizeof(recvBuf));
// FILE fp;
/ fp = fopen("C:\Users\liuwan\Desktop\web.html","w");
if(!fp)
{
printf("open file failed\n");
return 0;
}*/
char receive = NULL;
char *new_point = NULL; //零时指针
int temp = 0, temp1 = 0;
receive = (char)malloc(5); //开辟临时大小
memset(receive,0,5); //初始化
while(temp = recv(c,recvBuf,1000,0)) //循环接收 到结束返回零
{
if(temp != SOCKET_ERROR) //网络阻塞返回-1 不执行
{
temp1 += temp;
new_point = (char*)realloc(receive,temp1+2); //开辟适应此次加以前接收过的所有字符的空间大小(temp+2)
if(new_point == NULL) //开辟失败进入错误流程
{
printf("no have enough memory.\n");
free(receive);
return 0;
}
receive = new_point;
strcat(receive,recvBuf);
memset(recvBuf,0,sizeof(receive));//清空接收缓冲区,以便进行下一次接收
puts(receive);
}
memset(recvBuf,0,sizeof(recvBuf));
}
// fclose(fp);
printf("................................接收完毕..............................\n"); 
closesocket(c);
WSACleanup();
system("pause");
return 0;
}

展开
收起
a123456678 2016-03-20 14:08:26 2029 0
1 条回答
写回答
取消 提交回答
  • char receive = NULL;
    char *new_point = NULL; //零时指针
    int temp = 0, temp1 = 0;
    receive = (char)malloc(5); //开辟临时大小
    memset(receive,0,5); //初始化
    while(temp = recv(c,recvBuf,1000,0)) //循环接收 到结束返回零
    {
    
    if(temp != SOCKET_ERROR) //网络阻塞返回-1 不执行
    {
    temp1 += temp;
    new_point = (char*)realloc(receive,temp1+2); //开辟适应此次加以前接收过的所有字符的空间大小(temp+2)
    if(new_point == NULL) //开辟失败进入错误流程
    {
    printf("no have enough memory.\n");
    free(receive);
    return 0;
    }
    receive = new_point;
    strcat(receive,recvBuf);
    memset(recvBuf,0,sizeof(receive));//清空接收缓冲区,以便进行下一次接收
    puts(receive);
    }
    memset(recvBuf,0,sizeof(recvBuf));
    }
    
    
    2019-07-17 19:09:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
可编程网络视角的网络创新研究 立即下载
可编程网络和SDN3.0 立即下载
开放促进创新:构建开放网络生态 立即下载