TSEWU
TSEWU
采纳率0%
2016-06-29 07:54 阅读 7.8k

C/C++ socket tcp 发送和接收数据问题

客户端和服务器之间发送的一个数据正常,但当发送第二个数据后便出现了问题。求大神帮忙看一哈:
代码如下:
client端:
#include
#include
#include
using namespace std;
#pragma comment(lib,"ws2_32.lib")

int main(int argc, char* argv[])
{

WORD sockVersion = MAKEWORD(2,2);
WSADATA data;
if(WSAStartup(sockVersion, &data) != 0)
{
return 0;
}

SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sclient == INVALID_SOCKET)
{
    printf("invalid socket !");
    return 0;
}

sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(1000);
serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
/****************************************
int WSA_return;
WSADATA WSAData;
WSA_return=WSAStartup(0x0101,&WSAData);

hostent *host_entry;// 结构指针 
char host_name[256] ="manage.dgjgw.cn";

if(WSA_return==0)
{

    host_entry=gethostbyname(host_name);// 即要解析的域名或主机名 
    printf("%s\n", host_name);
    if(host_entry!=0)
    {
        printf("解析ip地址: ");
        printf("%s",inet_ntoa(*((struct in_addr*)host_entry->h_addr)));

    }

}
serAddr.sin_addr.S_un.S_addr = inet_addr((char*)inet_ntoa(*((struct in_addr*)host_entry->h_addr)));
****************************************/

//接收缓存区
int nRecvBuf=32*1024;//设置为32K
setsockopt(sclient,SOL_SOCKET,SO_RCVBUF,(const char*)&nRecvBuf,sizeof(int));
//发送缓冲区
int nSendBuf=32*1024;//设置为32K
setsockopt(sclient,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int));

printf("连接中》》》》》》》》》\n");
//连接1
if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
    printf("connect error !\n");
    closesocket(sclient);
    return 0;
}
else
{
    printf(" connect success !\n");
}
//连接2

while (true)
{  
    char sendData[10] ={0x05,0x01,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33};
    char sendData1[2] ={0x05,0x03};
    cout<<"请输入指令"<<endl;
    int a=0;
    cin>>a;
    switch(a)
    {
    case 1:
        printf("发送注册消息\n");
        send(sclient, sendData, strlen(sendData), 0);
        break;      
    case 2:
        printf("发送心跳消息\n");
        send(sclient, sendData1, strlen(sendData1), 0);
        break;
    default : 
        cout<<"输入错误"<<endl; 
        break;
    }
    char recData[2];
    memset(recData,0,2);
    int ret=0;
    ret = recv(sclient, recData, 2, 0);
    if(ret > 0)
    {
        printf("信息发送成功,回执如下\n");
        for(int i=0;i<=ret;i++)
        {
            printf("%x",recData[i]);
        }
        printf("\n");
    }
    else
    {
        printf("信息发送失败,关闭客户端\n");
        break;
    }
memset(recData,0,2);
closesocket(sclient);
}
system("pause");
closesocket(sclient);
WSACleanup();
return 0;

}
sever端:
//#include "stdafx.h"
#include
#include

#pragma comment(lib,"ws2_32.lib")

int main(int argc, char* argv[])
{
//初始化WSA
WORD sockVersion = MAKEWORD(2,2);
WSADATA wsaData;
if(WSAStartup(sockVersion, &wsaData)!=0)
{
return 0;
}

//创建套接字
SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(slisten == INVALID_SOCKET)
{
    printf("socket error !");
    return 0;
}

//绑定IP和端口
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(1000);
sin.sin_addr.S_un.S_addr = INADDR_ANY; 
if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
    printf("bind error !");
}      

//开始监听
if(listen(slisten, 5) == SOCKET_ERROR)
{
    printf("listen error !");
    return 0;
}

//循环接收数据
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255]; 
while (true)
{
    printf("等待连接...\n");
    sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
    if(sClient == INVALID_SOCKET)
    {
        printf("accept error !");
        continue;
    }
    printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
    //接收数据
    char revData[10];
    memset(revData,0,10);
    int ret= recv(sClient, revData, 10, 0);
    if(ret > 0)
    {   
        printf("消息接收成功:\n");
        for(int i=0;i<ret;i++)
        {   
            printf("%x",revData[i]);
        }
        printf("\n");
    }
    else
    {
      printf("消息接收失败:\n");
    }
    while(revData[0]==0x05&&revData[1]==0x01&&revData[2]==0x33&&revData[3]==0x33&&revData[4]==0x33
        &&revData[5]==0x33&&revData[6]==0x33&&revData[7]==0x33&&revData[8]==0x33&&revData[9]==0x33)
    {
        printf("注册消息接收成功:\n");
        for(int i=0;i<ret;i++)
        {   
            printf("%x",revData[i]);
        }
        printf("\n");
        char sendData1[2];
        sendData1[0]=0x05;
        sendData1[1]=0x02;
        send(sClient, sendData1, 2, 0);
        break;
    }
    while(revData[0]==0x05&&revData[1]==0x03)
    {
        printf("心跳消息接收成功:\n");
        for(int i=0;i<2;i++)
        {
            printf("%x",revData[i]);
        }
        printf("\n");
        char sendData[2];
        sendData[0]=0x05;
        sendData[1]=0x04;
        send(sClient, sendData, 2, 0);
        break;

    }

}
closesocket(sClient);
closesocket(slisten);
WSACleanup();
return 0;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • lx624909677 lx624909677 2016-06-29 08:32

    第二次发送出现什么问题?发送乱码还是发送失败或者其他?

    点赞 评论 复制链接分享
  • wangyaninglm shiter 2016-06-29 15:56

    对,我觉的楼主还是首先需要把问题定义清楚,弄清楚到底那里出现了这样的问题

    点赞 评论 复制链接分享

相关推荐