Skip to content

Instantly share code, notes, and snippets.

@lb5160482
Last active January 15, 2018 21:56
Show Gist options
  • Save lb5160482/9bee159f80c7c386a2536ea51f92ea32 to your computer and use it in GitHub Desktop.
Save lb5160482/9bee159f80c7c386a2536ea51f92ea32 to your computer and use it in GitHub Desktop.
TCP/IP c++
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
void main()
{
//¼ÓÔØÌ×½Ó×Ö
WSADATA wsaData;
char buff[1024];
memset(buff, 0, sizeof(buff));
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock");
return;
}
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(5555);
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//´´½¨Ì×½Ó×Ö
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == sockClient) {
printf("Socket() error:%d", WSAGetLastError());
return;
}
//Ïò·þÎñÆ÷·¢³öÁ¬½ÓÇëÇó
if (connect(sockClient, (struct sockaddr*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET) {
printf("Connect failed:%d", WSAGetLastError());
return;
}
else
{
//½ÓÊÕÊý¾Ý
recv(sockClient, buff, sizeof(buff), 0);
printf("%s\n", buff);
}
//·¢ËÍÊý¾Ý
strcpy(buff, "hello, this is a Client....");
//char buff = "hello, this is a Client....";
send(sockClient, buff, sizeof(buff), 0);
//¹Ø±ÕÌ×½Ó×Ö
closesocket(sockClient);
WSACleanup();
}
#include <WinSock2.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
void main()
{
WSADATA wsaData;
int port = 5556;
char buf[] = "Server: hello, I am a server.....";
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock");
return;
}
//´´½¨ÓÃÓÚ¼àÌýµÄÌ×½Ó×Ö
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(port); //1024ÒÔÉϵĶ˿ںÅ
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
int retVal = bind(sockSrv, (LPSOCKADDR)&addrSrv, sizeof(SOCKADDR_IN));
if (retVal == SOCKET_ERROR) {
printf("Failed bind:%d\n", WSAGetLastError());
return;
}
if (listen(sockSrv, 10) == SOCKET_ERROR) {
printf("Listen failed:%d", WSAGetLastError());
return;
}
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
//µÈ´ý¿Í»§ÇëÇóµ½À´
SOCKET sockConn = accept(sockSrv, (SOCKADDR *)&addrClient, &len);
if (sockConn == SOCKET_ERROR) {
printf("Accept failed:%d", WSAGetLastError());
}
printf("Accept client IP:[%s]\n", inet_ntoa(addrClient.sin_addr));
while (1)
{
char recvBuf[100];
memset(recvBuf, 0, sizeof(recvBuf));
// //½ÓÊÕÊý¾Ý
int iResult = recv(sockConn, recvBuf, sizeof(recvBuf), 0);
printf("%s\n", recvBuf);
//·¢ËÍÊý¾Ý
int iSend = send(sockConn, buf, sizeof(buf), 0);
if (iSend == SOCKET_ERROR) {
/*printf("send failed");
break;*/
sockConn = accept(sockSrv, (SOCKADDR *)&addrClient, &len);
if (sockConn == SOCKET_ERROR) {
printf("Accept failed:%d", WSAGetLastError());
}
printf("Accept client IP:[%s]\n", inet_ntoa(addrClient.sin_addr));
}
//closesocket(sockConn);
}
closesocket(sockSrv);
WSACleanup();
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment