VS2010写的第一个Unicode的程序
一直都是用VC6.0写的ANSI的代码,晚上把Unicode代码的写法了解了一下,写了下面这两个Demo....
当作学习Unicode socket代码的编写吧。。
UnicodeServer.cpp
#include <WinSock2.h>
#include <windows.h>
#include <strsafe.h>
#include <stdlib.h>
#include <atlbase.h>
#pragma comment(lib, "ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData))
{
_tprintf_s(_T("ERROR:WSAStart error.\n"));
return 0;
}
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
_tprintf_s(_T("ERROR:Socket error.\n"));
return 0;
}
USES_CONVERSION;
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr = *((in_addr*)gethostbyname(T2A(argv[1]))->h_addr);
if (connect(s, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
{
TCHAR szBuff[] = _T("你好,这是Unicode编码!\n");
int ret = send(s, (char*)szBuff, sizeof(TCHAR)*_tcsclen(szBuff), 0);
}
return 0;
}
UnicodeClient.cpp
#include <WinSock2.h>
#include <windows.h>
#include <strsafe.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData))
{
_tprintf_s(_T("ERROR:WSAStart error.\n"));
return 0;
}
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
_tprintf_s(_T("ERROR:Socket error.\n"));
return 0;
}
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.S_un.S_addr = INADDR_ANY;
sin.sin_port = htons(8888);
if (bind(s, (SOCKADDR*)&sin, sizeof(sin)))
{
_tprintf_s(_T("ERROR:Bind port error.\n"));
return 0;
}
if (listen(s, 5))
{
_tprintf_s(_T("ERROR:Listen error.\n"));
return 0;
}
SOCKET sAccept = accept(s, NULL, NULL);
if (sAccept == INVALID_SOCKET)
{
_tprintf_s(_T("ERROR:Listen error.\n"));
}
TCHAR szBuff[1024] = {0};
recv(sAccept, (char*)szBuff, 1024*sizeof(TCHAR), 0);
MessageBox(NULL, szBuff, _T(""), MB_OK);
_tsystem(_T("pause"));
return 0;
}
摘自:_ccSec blog
补充:软件开发 , 其他 ,