| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 508 人关注过本帖
标题:一个简易聊天程序,为什么只能正常发送接收两次,然后服务器就收不到客户端 ...
只看楼主 加入收藏
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
收藏
 问题点数:0 回复次数:0 
一个简易聊天程序,为什么只能正常发送接收两次,然后服务器就收不到客户端发的内容呢?
#include <winsock.h>
#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>

 int main()
{
   
     
     WORD wVersionRequested;
     WSADATA wsaData;
     wVersionRequested = MAKEWORD( 1, 1 );
     
     int err;
     err = WSAStartup( wVersionRequested, &wsaData );
     if ( err != 0 ) {
     /* Tell the user that we couldn't find a useable */
     /* winsock.dll. */
     return(0);
     }
       int c,d,e,g,i,n,q,z,k;
       SOCKET a,b;
     char s[30];
     
     struct sockaddr_in my_addr;
     
         n=strlen(s);
         
     
     struct sockaddr_in remote_addr;
     
     a=socket(AF_INET,SOCK_STREAM,0);//调用Socket函数//
     
     

   
     
     
   
    if(a==-1)
    {
        printf("创建SOCKET失败",NULL,MB_OK);
    }
    memset(&my_addr,0,sizeof(my_addr));
    my_addr.sin_family=AF_INET;//本地主机使用协议//
    my_addr.sin_port=htons(80);//本地主机端口//
    my_addr.sin_addr.S_un.S_addr = inet_addr("172.17.36.25");//本机IP//
   

    if(bind(a,( struct sockaddr*)&my_addr,sizeof(my_addr))==-1)
    {
           printf("错误码%d",GetLastError());
    }
   
     c=listen(a,10);//调用listen()函数监听端口//
     if(c==-1)
     {
         printf("错误码%d",GetLastError());//出错提示//
         
     }
     else
     {
         printf("服务器已启动!\n");
     }
  
     d=sizeof(remote_addr);
     

     printf("等待连接...\n");
     b=accept(a,(struct sockaddr*)&remote_addr,&d);//accept()函数,用于接受客户端的连接请求//
     if(b!=-1)
     {
      printf("已连接上!\n");
     }
     while(b!=-1)
     {
         
         scanf("%s",&s);//输入数据,并发送//
         printf("发送中...\n");
         send(b,s,sizeof(s),0);
         printf("发送成功!\n");
         recv(b,s,sizeof(s),0);//发送完毕,准执行此函数准备接收数据//
         printf("%s\n",s);//显示收到的数据//
         
         
     }
      
     
     WSACleanup();//释放资源//
     closesocket(a);//关闭socket//
     closesocket(b);//关闭socket1//
   
     
 }
#include <winsock.h>
#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>

 int main()
{
     int c,d,e,f,g,k,n,z,i;
     char s[66]="234";
     
     WORD wVersionRequested;
     WSADATA wsaData;
     wVersionRequested = MAKEWORD( 1, 1 );
     SOCKET a,b;
     g = WSAStartup( wVersionRequested, &wsaData );
     if ( g != 0 ) {
     /* Tell the user that we couldn't find a useable */
     /* winsock.dll. */
     return(0);
     }
     
     struct sockaddr_in remote_addr,my_addr;
     
     a=socket(AF_INET,SOCK_STREAM,0);//调用Socket函数//
     
     if(a==-1)
     {
        printf("创建SOCKET失败,错误码 %d",GetLastError());
     }
     memset(&remote_addr,0,sizeof(remote_addr));
     remote_addr.sin_family=AF_INET;//远程主机使用协议//
     remote_addr.sin_port=htons(80);//远程主机端口//
     remote_addr.sin_addr.S_un.S_addr = inet_addr("172.17.36.25");//远程主机IP//
     
     printf("客户端已启动!\n");
     
     
     
     printf("正在连接...\n");
     
      c=connect(a,(struct sockaddr*)&remote_addr,sizeof(remote_addr));//向服务器发出连接请求//
     
     if(c==-1)
     {
       printf("连接失败! %d\n",GetLastError());
       exit(1);
     }
     else
     {
         printf("连接成功!\n");
     }
     while(c!=-1)//循环发送接收//
     {
         
         recv(a,s,sizeof(s),0);//接收客户断发送的数据,并显示//
   
         printf("%s\n",s);
         scanf("%s",&s);
         printf("发送中..\n");
         send(a,s,sizeof(s),0);//发送信息//
         printf("发送成功!\n");
     }
     closesocket(a);//关闭socket//
     WSACleanup();//释放占用资源//
     
 }

搜索更多相关主题的帖子: 客户端 服务器 comment include 
2011-11-13 16:38
快速回复:一个简易聊天程序,为什么只能正常发送接收两次,然后服务器就收不到客 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014776 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved