C#??????windowsAPI?????
???????????? ???????[ 2016/4/7 11:36:14 ] ??????????????????? .NET
????windows API????????
//Server.cpp
#include <iostream>
#include <winsock2.h>
using namespace std;
#pragma comment(lib?? "ws2_32.lib")
#define PORT 8889
#define IP_ADDRESS "127.0.0.1"
DWORD WINAPI ClientThread(LPVOID lpParameter)
{
SOCKET CientSocket = (SOCKET)lpParameter;
int Ret = 0;
char RecvBuffer[MAX_PATH];
while (true)
{
memset(RecvBuffer?? 0x00?? sizeof(RecvBuffer));
Ret = recv(CientSocket?? RecvBuffer?? MAX_PATH?? 0);
if (Ret == 0 || Ret == SOCKET_ERROR)
{
cout << "????????!" << endl;
break;
}
cout << "????????????:" << RecvBuffer << endl;
}
return 0;
}
int main(int argc?? char* argv[])
{
WSADATA Ws;
SOCKET ServerSocket?? ClientSocket;
struct sockaddr_in LocalAddr?? ClientAddr;
int Ret = 0;
int AddrLen = 0;
HANDLE hThread = NULL;
//Init Windows Socket
if (WSAStartup(MAKEWORD(2?? 2)?? &Ws) != 0)
{
cout << "Init Windows Socket Failed::" << GetLastError() << endl;
return -1;
}
//Create Socket
ServerSocket = socket(AF_INET?? SOCK_STREAM?? IPPROTO_TCP);
if (ServerSocket == INVALID_SOCKET)
{
cout << "Create Socket Failed::" << GetLastError() << endl;
return -1;
}
LocalAddr.sin_family = AF_INET;
LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
LocalAddr.sin_port = htons(PORT);
memset(LocalAddr.sin_zero?? 0x00?? 8);
//Bind Socket
Ret = bind(ServerSocket?? (struct sockaddr*)&LocalAddr?? sizeof(LocalAddr));
if (Ret != 0)
{
cout << "Bind Socket Failed::" << GetLastError() << endl;
return -1;
}
//listen
Ret = listen(ServerSocket?? 10);
if (Ret != 0)
{
cout << "listen Socket Failed::" << GetLastError() << endl;
return -1;
}
cout << "???????????" << endl;
while (true)
{
AddrLen = sizeof(ClientAddr);
ClientSocket = accept(ServerSocket?? (struct sockaddr*)&ClientAddr?? &AddrLen);
if (ClientSocket == INVALID_SOCKET)
{
cout << "Accept Failed::" << GetLastError() << endl;
break;
}
cout << "?????????::" << inet_ntoa(ClientAddr.sin_addr) << ":" << ClientAddr.sin_port << endl;
hThread = CreateThread(NULL?? 0?? ClientThread?? (LPVOID)ClientSocket?? 0?? NULL);
if (hThread == NULL)
{
cout << "Create Thread Failed!" << endl;
break;
}
CloseHandle(hThread);
}
closesocket(ServerSocket);
closesocket(ClientSocket);
WSACleanup();
return 0;
}
????windows API?????
// Client.cpp
#include <iostream>
#include <winsock2.h>
using namespace std;
#pragma comment(lib?? "ws2_32.lib")
#define PORT 8889
#define IP_ADDRESS "127.0.0.1"
int main(int argc?? char * argv[])
{
WSADATA Ws;
SOCKET ClientSocket;
struct sockaddr_in ServerAddr;
int Ret = 0;
int AddrLen = 0;
HANDLE hThread = NULL;
char SendBuffer[MAX_PATH];
//Init Windows Socket
if (WSAStartup(MAKEWORD(2?? 2)?? &Ws) != 0)
{
cout << "Init Windows Socket Failed::" << GetLastError() << endl;
return -1;
}
//Create Socket
ClientSocket = socket(AF_INET?? SOCK_STREAM?? IPPROTO_TCP);
if (ClientSocket == INVALID_SOCKET)
{
cout << "Create Socket Failed::" << GetLastError() << endl;
return -1;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
ServerAddr.sin_port = htons(PORT);
memset(ServerAddr.sin_zero?? 0x00?? 8);
Ret = connect(ClientSocket?? (struct sockaddr*)&ServerAddr?? sizeof(ServerAddr));
if (Ret == SOCKET_ERROR)
{
cout << "Connect Error::" << GetLastError() << endl;
return -1;
}
else
{
cout << "??????!" << endl;
}
while (true)
{
cin.getline(SendBuffer?? sizeof(SendBuffer));
Ret = send(ClientSocket?? SendBuffer?? (int)strlen(SendBuffer)?? 0);
if (Ret == SOCKET_ERROR)
{
cout << "Send Info Error::" << GetLastError() << endl;
break;
}
}
closesocket(ClientSocket);
WSACleanup();
return 0;
}
???????????????????????????????????4????????
????htons()??htonl()??ntohs()??ntohl()
????h???host?????n???net?????s???short??l???long??
????htons???????????????????????????????????? ?????????????洢??????????λ??????????????????
????????????????TCP/IP?й漲?????????????????????????CPU??????????????????????????????????????????????????????????????????????big-endian???????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
???????C#?е?StringC# Socket???????????????????C#?????????????????????????C#???????C#????????????Log4net??????δ????C#?????????????C#?????????????????C#???????????????C#????????????????C#7 ?е?Tuple??????C#??TypeScript - GeneratorC# ?????????細(xì)??????????C#?е???н???C#??MySQL?????????μ??Ч?????д??C#????C++??dll???C#??VS2010????е??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????