借鉴,源码非原创,来自MagicSaring的一个简单的TCP通讯程序Demo。html
1、客户端源码:
ios
#include
#include
#include "client.h"
using namespace std;
#pragma comment(lib, "WS2_32.lib")
//表示连接WS2_32.lib这个库。
#define PORT 1024
int connectToServer(char* send_msg)
{
SOCKET sock_client;
struct sockaddr_in server_addr;
int addr_len = sizeof(struct sockaddr_in);
char msgbuffer[1000];
memset(msgbuffer, 0, sizeof(msgbuffer));
// 初始化 winsock2.dll[12/27/2017 MagicScaring]
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2); //生成版本号
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
cout << "加载 winsock.dll失败" << endl;
return 0;
}
// 建立套接字 [12/27/2017 MagicScaring]
if ((sock_client = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
{
cout << "建立套接字失败! 错误代码:" << WSAGetLastError() << endl;
WSACleanup(); //注销WinSock动态连接库
return 0;
}
// 填写服务器地址 [本机地址]
char IP[20] = { "192.168.1.106" };
/*char IP[20];
cout << "输入服务器地址:" << endl;
cin >> IP;*/
memset((void*)&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(IP);
// 与服务器创建链接 [2018/5/22]
if (connect(sock_client, (struct sockaddr*)&server_addr, addr_len) == SOCKET_ERROR)
{
cout << "链接失败! 错误代码:" << WSAGetLastError() << endl;
closesocket(sock_client);
WSACleanup();
return 0;
}
while (true)
{
int size;
if ((size = recv(sock_client, msgbuffer, sizeof(msgbuffer), 0)) == SOCKET_ERROR)
{
cout << "接收信息失败! 错误代码:" << WSAGetLastError() << endl;
closesocket(sock_client);
WSACleanup();
return 0;
}
else if (size == 0)
{
cout << "对方已关闭链接" << endl;
closesocket(sock_client);
WSACleanup();
return 0;
}
else
{
cout << "The message from Server:" << msgbuffer << endl;
}
//char send_msg[1000] = { "Hello server" };
//send_msg是要发送的数据,是char指针
if ((size = send(sock_client, send_msg, sizeof(send_msg), 0)) == SOCKET_ERROR)
{
cout << "发送信息失败! 错误代码:" << WSAGetLastError() << endl;
}
else if (size == 0)
{
cout << "对方已关闭链接" << endl;
}
else
{
cout << "信息发送成功" << endl;
}
}
closesocket(sock_client);
WSACleanup();
return 0;
}
2、主程序调用服务器
connectToServer(s_buf);
注意主程序头文件库的顺序,winSock2.h放在最前面。socket
至于为何,I dont know.函数
3、服务器源码spa
#include "stdafx.h"
#include
#include
using namespace std;
#pragma comment(lib, "WS2_32.lib")
//表示连接WS2_32.lib这个库。
#define PORT 1024
int main()
{
SOCKET sock_server, newsock;
struct sockaddr_in server_addr, client_addr;
char msg[] = { "Hello client" };
// 初始化 winsock2.dll[12/27/2017 MagicScaring]
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2); //生成版本号
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
cout << "加载 winsock.dll失败" << endl;
return 0;
}
// 建立套接字 [12/27/2017 MagicScaring]
if ((sock_server = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
{
cout << "建立套接字失败! 错误代码:" << WSAGetLastError() << endl;
WSACleanup(); //注销WinSock动态连接库
return 0;
}
// 填写须要绑定的本地地址 []
int addr_len = sizeof(struct sockaddr_in);
memset((void*)&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock_server, (struct sockaddr*)&server_addr, addr_len) != 0)
{
cout << "绑定失败!错误代码:" << WSAGetLastError() << endl;
closesocket(sock_server); //关闭已链接套接字
WSACleanup(); //注销WinSock动态连接库
return 0;
}
// 开始监听 [12/27/2017 MagicScaring]
if (listen(sock_server, 0) != 0)
{
cout << "listen调用失败!错误代码:" << WSAGetLastError() << endl;
closesocket(sock_server);
WSACleanup();
return 0;
}
else
{
cout << "listening...." << endl;
}
// 循环:接收链接请求并收发数据 [12/27/2017 MagicScaring]
int size;
while (true)
{
if ((newsock = accept(sock_server, (struct sockaddr *)&client_addr, &addr_len)) == INVALID_SOCKET)
{
cout << "accept 函数调用失败! 错误代码:" << WSAGetLastError() << endl;
break;
}
else
{
cout << "成功接收到一个链接请求!" << endl;
size = send(newsock, msg, sizeof(msg), 0);
if (size == SOCKET_ERROR)
{
cout << "发送信息失败! 错误代码:" << WSAGetLastError() << endl;
closesocket(newsock);
continue;
}
else if (size == 0)
{
cout << "对方已关闭链接" << endl;
closesocket(newsock);
continue;
}
else
{
cout << "信息发送成功" << endl;
}
size = recv(newsock, msg, sizeof(msg), 0);
if (size == SOCKET_ERROR)
{
cout << "接收信息失败! 错误代码:" << WSAGetLastError() << endl;
closesocket(newsock);
WSACleanup();
return 0;
}
else if (size == 0)
{
cout << "对方已关闭链接" << endl;
closesocket(newsock);
WSACleanup();
return 0;
}
else
{
cout << "The message from Client:" << msg<< endl;
}
}
}
return 0;
}