#include “stdafx.h”
#include <string>
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
#pragma comment(lib, “ws2_32.lib”)
int main(int argc, char* argv[])
{
printf(“Hello World!\n”);
system(“pause”);
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN sa;
sa.sin_addr.S_un.S_addr = inet_addr(“127.0.0.1”);
sa.sin_family = AF_INET;
sa.sin_port = htons(8080);
int timeout = 5000;
setsockopt(s,SOL_SOCKET,SO_SNDTIMEO,(const char*)&timeout,sizeof(timeout));
int nResult = connect(s, (SOCKADDR*)&sa, sizeof(SOCKADDR));
if(nResult == SOCKET_ERROR)
printf(“超时\n”);
else
printf(“未超时\n”);
const char *cmd = “username\r\n”
“nick\r\n”
“tom\r\n”
“jack\r\n”
“linda\r\n”
“bruce\r\n”
“michle\r\n\r\n”;
printf(“发送:%d\n”, send(s, cmd, strlen(cmd) + 1, 0));
char szBuf[0xFFFF];
std::string recv_pack;
for(;;)
{
nResult = recv(s, szBuf, 0xFFFF, 0);
if(nResult <= 0)
break;
szBuf[nResult] = 0;
recv_pack += szBuf;
}
if(recv_pack.length())
{
printf(“%s\n”, recv_pack.c_str());
FILE *fp = fopen(“C:\gps.txt”, “w+”);
fwrite(recv_pack.c_str(), recv_pack.length(), 1, fp);
fclose(fp);
}
closesocket(s);
system(“pause”);
return 0;
}
问题
怎么样将要发送的那些字符串以UTF-8的格式进行发送,谢谢
编译环境 c++
#include <string>
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
#pragma comment(lib, “ws2_32.lib”)
int main(int argc, char* argv[])
{
printf(“Hello World!\n”);
system(“pause”);
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN sa;
sa.sin_addr.S_un.S_addr = inet_addr(“127.0.0.1”);
sa.sin_family = AF_INET;
sa.sin_port = htons(8080);
int timeout = 5000;
setsockopt(s,SOL_SOCKET,SO_SNDTIMEO,(const char*)&timeout,sizeof(timeout));
int nResult = connect(s, (SOCKADDR*)&sa, sizeof(SOCKADDR));
if(nResult == SOCKET_ERROR)
printf(“超时\n”);
else
printf(“未超时\n”);
const char *cmd = “username\r\n”
“nick\r\n”
“tom\r\n”
“jack\r\n”
“linda\r\n”
“bruce\r\n”
“michle\r\n\r\n”;
printf(“发送:%d\n”, send(s, cmd, strlen(cmd) + 1, 0));
char szBuf[0xFFFF];
std::string recv_pack;
for(;;)
{
nResult = recv(s, szBuf, 0xFFFF, 0);
if(nResult <= 0)
break;
szBuf[nResult] = 0;
recv_pack += szBuf;
}
if(recv_pack.length())
{
printf(“%s\n”, recv_pack.c_str());
FILE *fp = fopen(“C:\gps.txt”, “w+”);
fwrite(recv_pack.c_str(), recv_pack.length(), 1, fp);
fclose(fp);
}
closesocket(s);
system(“pause”);
return 0;
}
问题
怎么样将要发送的那些字符串以UTF-8的格式进行发送,谢谢
编译环境 c++
解决方案
5
仅供参考,尽管是VB6:
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long "常用的代码页: const cpUTF8 =65001 const cpGB2312 = 936 const cpGB18030=54936 const cpUTF7 =65000 Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String Dim bufSize As Long bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0) MultiByteToUTF16 = Space(bufSize) MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize End Function Function UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte() Dim bufSize As Long Dim arr() As Byte bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0) ReDim arr(bufSize - 1) WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0 UTF16ToMultiByte = arr End Function Private Sub Command1_Click() MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8) End Sub
5
int ToUTF8(LPTSTR str,char** buff){ //将输入字符串转换成Unicode LPWSTR ustr = NULL; #ifdef UNICODE ustr = str; #else int ulen = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0); if(ulen == 0) return 0; ustr = new wchar_t[ulen]; if(MultiByteToWideChar(CP_ACP,0,str,-1,ustr,ulen) == 0){ delete[] ustr; return 0; } #endif //将Unicode处理成UTF8 int blen = WideCharToMultiByte(CP_UTF8,0,ustr,-1,NULL,0,NULL,NULL); if(blen == 0){ #ifndef UNICODE delete[] ustr; #endif return 0; } char* bstr = new char[blen]; blen = WideCharToMultiByte(CP_UTF8,0,ustr,-1,bstr,blen,NULL,NULL); if(blen == 0){ delete[] bstr; bstr = NULL; } #ifndef UNICODE delete[] ustr; #endif *buff = bstr; return blen - 1; //为什么减1查看API参考 }
5
char* ustr = NULL; int len = ToUTF8(TEXT("你好abc"),&ustr); //有长度和缓冲了,尽情用吧 //用完后销毁 delete[] ustr;
5
用MultiByteToWideChar和WideCharToMultiByte函数