客户端: 1、头文件 (1)clientReceive.h
#pragma once
#ifndef _ser_listen_h_
#define _ser_listen_h_
void* clientReceive(void* arg);
#endif
(2)message.h
#pragma once
#ifndef _message_h_
#define _message_h_
#define PORT 8888
struct message
{
char message[32];
short int startData;
short int sizeData;
short int serverPort;
int serverIP;
short int clientPort;
int clientIP;
short int endData;
};
#endif
2、源文件 (1)tcpClient.cpp:
#include <winsock2.h>
#include <iostream>
#include <Ws2tcpip.h>
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
#include <STDIO.H>
#include <string>
# include <stdlib.h>
#include"message.h"
#include"clientReceive.h"
int main(int argc, char* argv[])
{
pthread_t threadClientReceive;
WORD sockVersion = MAKEWORD(2, 2);
WSADATA data;
if (WSAStartup(sockVersion, &data) != 0)
{
return 0;
}
SOCKET clientReceived = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientReceived == INVALID_SOCKET)
{
printf("invalid socket !");
return 0;
}
struct sockaddr_in client;
client.sin_family = AF_INET;
client.sin_port = htons(PORT);
client.sin_addr.S_un.S_addr = inet_addr("127.0.1.1");
if (bind(clientReceived, (struct sockaddr*)&client, sizeof(client)) < 0)
{
printf("bind error !");
}
struct sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(8888);
serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (connect(clientReceived, (struct sockaddr*)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("connect error !");
closesocket(clientReceived);
return 0;
}
message messageClient =
{
{ '1','1','1','1' },
0x5002,
sizeof(messageClient.message),
0x8888,
inet_addr("127.0.1.1"),
0x1818,
inet_addr("127.0.0.1"),
0x5003
};
char dataClient[64];
::memset(dataClient, 0, 64);
::memcpy(dataClient, &messageClient.startData, 2);
::memcpy(dataClient + 2, &messageClient.sizeData, 2);
::memcpy(dataClient + 4, &messageClient.serverPort, 2);
::memcpy(dataClient + 6, &messageClient.serverIP, 4);
::memcpy(dataClient + 10, &messageClient.clientPort, 2);
::memcpy(dataClient + 12, &messageClient.serverIP, 4);
::memcpy(dataClient + 16, &messageClient.message, messageClient.sizeData);
::memcpy(dataClient + 16 + messageClient.sizeData, &messageClient.endData, 2);
const char* sendData = (char*)(dataClient);
pthread_create(&threadClientReceive, NULL, clientReceive, (void*)clientReceived);
while(1)
{
send(clientReceived, sendData, sizeof(dataClient), 0);
Sleep(1000);
}
closesocket(clientReceived);
WSACleanup();
system("pause");
return 0;
}
(2)clientRecive.cpp
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#include"clientReceive.h"
void* clientReceive(void* arg)
{
int receiveClient = (int)arg;
char recData[255];
while (1)
{
int ret = recv(receiveClient, recData, 64, 0);
if (ret > 0)
{
recData[ret] = 0x00;
printf(recData);
}
Sleep(1000);
}
return NULL;
}
服务器: 1、头文件 (1)serverReceive.h
#pragma once
#ifndef _ser_listen_h_
#define _ser_listen_h_
#define PORT 8888
void* serverReceive(void* arg);
#endif
(2)serverReceiveData.h
#pragma once
#ifndef _serverReceiveData_h_
#define _serverReceiveData_h_
struct serverReceiveData
{
unsigned short int startData;
unsigned short int sizeData;
unsigned short int serverPort;
struct in_addr serverIP;
unsigned short int clientPort;
struct in_addr clientIP;
char message[32];
unsigned short int endData;
}dataSever;
#endif
2、源文件 (1)tcpSever.cpp
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#include"serverReceive.h"
int main(int argc, char* argv[])
{
pthread_t threadSeverRecive;
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}
SOCKET serverReceived = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverReceived == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
struct sockaddr_in sever;
sever.sin_family = AF_INET;
sever.sin_port = htons(PORT);
sever.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(serverReceived, (struct sockaddr*)&sever, sizeof(sever)) < 0)
{
printf("bind error !");
}
if (listen(serverReceived, 5) == SOCKET_ERROR)
{
printf("listen error !");
return 0;
}
SOCKET severClient;
struct sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
printf("等待连接...\n");
severClient = accept(serverReceived, (struct sockaddr*)&remoteAddr, &nAddrlen);
if (severClient == INVALID_SOCKET)
{
printf("accept error !");
}
printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
pthread_create(&threadSeverRecive, NULL, serverReceive, (void*)severClient);
while (1)
{
const char* sendData = (char*)"111111111111\n";
send(severClient, sendData, strlen(sendData), 0);
Sleep(1000);
}
closesocket(serverReceived);
WSACleanup();
system("pause");
return 0;
}
(2)serverReceive.cpp
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <pthread.h>
#pragma comment(lib,"pthreadVC2.lib")
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#include"serverReceiveData.h"
#include"serverReceive.h"
void* serverReceive(void* arg)
{
char revData[255];
int ret = 0;
int serverClient = (int)arg;
while (1)
{
ret = recv(serverClient, revData, 255, 0);
if(ret>0)
{
revData[ret] = 0x00;
memset(&dataSever, 0, sizeof(dataSever));
memcpy(&dataSever.startData, revData, 2);
memcpy(&dataSever.sizeData, revData + 2, 2);
memcpy(&dataSever.serverPort, revData + 4, 2);
memcpy(&dataSever.serverIP, revData + 6, 4);
memcpy(&dataSever.clientPort, revData + 10, 2);
memcpy(&dataSever.clientIP, revData + 12, 4);
memcpy(&dataSever.message, revData + 16, 32);
memcpy(&dataSever.endData, revData + 16 + 32, 2);
printf("帧起始:%02x\n", dataSever.startData);
printf("帧长度:%d\n", dataSever.sizeData);
printf("源端口号:%02x\n", dataSever.serverPort);
printf("源ip:%s\n", inet_ntoa(dataSever.serverIP));
printf("目的端口号:%02x\n", dataSever.clientPort);
printf("目的ip:%s\n", inet_ntoa(dataSever.clientIP));
printf("传输数据:%s\n", dataSever.message);
printf("帧结束:%02x\n", dataSever.endData);
}
Sleep(1000);
}
return NULL;
}
|