本文介绍了无法接收完整的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在21天内按照Sam21学习VC ++ 6.0编写了一个简单的套接字程序。我有一个服务器和一个客户端。我为发送按钮和接收编写了处理程序。当我发送一个字符串时,我只收到接收方字符串的第一个字母。我想收到整个字符串。这是我的代码: 发送:: void CsocketDlg :: OnClickedBsend() { // TODO:添加你的控制通知处理程序代码在这里 int iLen; int iSent; // 带变量的同步控件 UpdateData(TRUE); // if(m_dSetThresholdFineDfLB.Create()== IDOK) // m_strMessage = m_dSetThresholdFineDfLB.m_sFrameLB; / / UpdateData(FALSE); if (m_strMessage!= ) { iLen = m_strMessage.GetLength(); // 发送消息 iSent = m_sConnectSocket.Send(LPCTSTR(m_strMessage) ),艾朗); // 我们是否能够发送消息 if (iSent == SOCKET_ERROR) {} else { // 将消息添加到列表框 m_ctlSent.AddString(m_strMessage ); // 将变量与控件同步 UpdateData(FALSE); } } } 收到:: void CsocketDlg :: OnReceive( void ) { char * pBuf = new char [ 1025 ]; int iBufSize = 1024 ; int iRcvd; CString strRecvd; // 收到消息 iRcvd = m_sConnectSocket.Receive(pBuf,iBufSize ); // 我们收到了什么吗? if (iRcvd == SOCKET_ERROR) {} else { // 截断消息的结尾 pBuf [iRcvd] = NULL; // 将邮件复制到CString strRecvd = pBuf; // 将消息添加到收到的列表框 m_ctlRecvd.AddString( strRecvd); // 将变量与控件同步 UpdateData(FALSE); } } 解决方案 从使用unicode更改为在项目属性中使用多字节。 I wrote a simple socket program by following Sam21 learn VC++ 6.0 in 21 days. I have a server and a client. I wrote handlers for send button and receive. When I send a string I am receiving only first letter of the string on the receive side. I wanna receive the entire string. This is my code:Send::void CsocketDlg::OnClickedBsend(){ // TODO: Add your control notification handler code here int iLen; int iSent; //sync controls with variables UpdateData(TRUE); //if(m_dSetThresholdFineDfLB.Create()==IDOK) //m_strMessage = m_dSetThresholdFineDfLB.m_sFrameLB; //UpdateData(FALSE); if(m_strMessage != " ") { iLen = m_strMessage.GetLength(); //send the message iSent = m_sConnectSocket.Send(LPCTSTR(m_strMessage),iLen); //were we able to send the message if(iSent == SOCKET_ERROR) { } else { //Add the message to the list box m_ctlSent.AddString(m_strMessage); //sync the variables with the control UpdateData(FALSE); } }}Receive::void CsocketDlg::OnReceive(void){ char *pBuf = new char[1025]; int iBufSize = 1024; int iRcvd; CString strRecvd; //receive the message iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize); //did we receive anything? if(iRcvd == SOCKET_ERROR) { } else { //truncate the end of the message pBuf[iRcvd] = NULL; //copy the message to CString strRecvd = pBuf; //add the message to the received list box m_ctlRecvd.AddString(strRecvd); //sync the variables with the controls UpdateData(FALSE); }} 解决方案 Changed from use unicode to use multi-byte in project properties. 这篇关于无法接收完整的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 15:34