/*
TCP Echo server example in winsock
Live Server on port 8888
*/
#include<stdio.h>
#include<winsock2.h>
#include "mbconfig.h"
#pragma comment(lib, "ws2_32.lib") //Winsock Library
int main(int argc, char* argv[])
{
WSADATA wsa;
SOCKET master, new_socket, client_socket[30], s;
struct sockaddr_in server, address;
int max_clients = 30, activity, addrlen, i, valread;
char* message = "ECHO Daemon v1.0 \r\n";
int sendLen = 0;
//size of our receive buffer, this is string length.
int MAXRECV = 1024;
//set of socket descriptors
fd_set readfds;
//1 extra for null character, string termination
//char* buffer;
//char* sendBuf;
//buffer = (char*)malloc((MAXRECV + 1) * sizeof(char));
//sendBuf = (char*)malloc((MAXRECV + 1) * sizeof(char));
uint8_t buffer[50];
uint8_t sendBuf[50];
for (i = 0; i < 30; i++)
{
client_socket[i] = 0;
}
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//Create a socket
if ((master = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(502);
//Bind
if (bind(master, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//Listen to incoming connections
listen(master, 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
addrlen = sizeof(struct sockaddr_in);
while (TRUE)
{
//clear the socket fd set
FD_ZERO(&readfds);
//add master socket to fd set
FD_SET(master, &readfds);
//add child sockets to fd set
for (i = 0; i < max_clients; i++)
{
s = client_socket[i];
if (s > 0)
{
FD_SET(s, &readfds);
}
}
//wait for an activity on any of the sockets, timeout is NULL , so wait indefinitely
activity = select(0, &readfds, NULL, NULL, NULL);
if (activity == SOCKET_ERROR)
{
printf("select call failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
//If something happened on the master socket , then its an incoming connection
if (FD_ISSET(master, &readfds))
{
if ((new_socket = accept(master, (struct sockaddr*)&address, (int*)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d \n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
//send new connection greeting message
if (send(new_socket, message, strlen(message), 0) != strlen(message))
{
perror("send failed");
}
puts("Welcome message sent successfully");
//add new socket to array of sockets
for (i = 0; i < max_clients; i++)
{
if (client_socket[i] == 0)
{
client_socket[i] = new_socket;
printf("Adding to list of sockets at index %d \n", i);
break;
}
}
}
//else its some IO operation on some other socket :)
for (i = 0; i < max_clients; i++)
{
s = client_socket[i];
//if client presend in read sockets
if (FD_ISSET(s, &readfds))
{
//get details of the client
getpeername(s, (struct sockaddr*)&address, (int*)&addrlen);
//Check if it was for closing , and also read the incoming message
//recv does not place a null terminator at the end of the string (whilst printf %s assumes there is one).
valread = recv(s, buffer, MAXRECV, 0);
if (valread == SOCKET_ERROR)
{
int error_code = WSAGetLastError();
if (error_code == WSAECONNRESET)
{
//Somebody disconnected , get his details and print
printf("Host disconnected unexpectedly , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
//Close the socket and mark as 0 in list for reuse
closesocket(s);
client_socket[i] = 0;
}
else
{
printf("recv failed with error code : %d", error_code);
}
}
if (valread == 0)
{
//Somebody disconnected , get his details and print
printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
//Close the socket and mark as 0 in list for reuse
closesocket(s);
client_socket[i] = 0;
}
//Echo back the message that came in
else
{
//add null character, if you want to use with printf/puts or other string handling functions
buffer[valread] = '\0';
/*printf("%s:%d - %s \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port), buffer);
send(s, buffer, valread, 0);*/
sendLen = ParsingClientAccessCommand(buffer, sendBuf);
send(s, sendBuf, sendLen, 0);
printf("接收到的数据长度:%d ,发送的数据长度:%d\n", valread, sendLen);
}
}
}
}
closesocket(s);
WSACleanup();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TCPApplication.rar (75个子文件)
TCPApplication
TCPServer
TCPServer.vcxproj.user 168B
TCPServer.vcxproj.filters 4KB
TCPServer.vcxproj 9KB
x64
Debug
mbascii.obj 16KB
mbpdu.obj 18KB
tcpserver.obj 31KB
mbtcpserver.obj 18KB
mbrtu.obj 10KB
mbrtumaster.obj 22KB
mbasciislave.obj 19KB
vc142.pdb 116KB
TCPServer.exe.recipe 273B
mbtcp.obj 7KB
mbcommon.obj 20KB
mbrtuslave.obj 18KB
TCPServer.log 198B
mbasciimaster.obj 24KB
vc142.idb 291KB
mbtcpclient.obj 26KB
TCPServer.tlog
CL.write.1.tlog 9KB
TCPServer.lastbuildstate 167B
link.command.1.tlog 5KB
CL.read.1.tlog 56KB
CL.command.1.tlog 8KB
link.write.1.tlog 2KB
link.read.1.tlog 6KB
tcpserver.c 6KB
TCPClient
TCPClientDlg.cpp 8KB
res
TCPClient.rc2 674B
TCPClient.ico 66KB
TCPClient.cpp 3KB
TCPClient.vcxproj.user 235B
TCPClient.vcxproj 11KB
TCPClient.vcxproj.filters 3KB
framework.h 2KB
resource.h 1009B
pch.cpp 158B
TCPClientDlg.h 985B
targetver.h 299B
pch.h 544B
x64
Debug
mbascii.obj 16KB
mbpdu.obj 18KB
TCPClient.tlog
CL.write.1.tlog 7KB
rc.command.1.tlog 464B
link.command.1.tlog 4KB
CL.read.1.tlog 353KB
CL.command.1.tlog 9KB
rc.read.1.tlog 4KB
link.write.1.tlog 2KB
TCPClient.lastbuildstate 167B
rc.write.1.tlog 274B
link.read.1.tlog 10KB
TCPClient.log 504B
TCPClient.obj 469KB
mbtcpserver.obj 18KB
mbrtu.obj 10KB
mbrtumaster.obj 22KB
mbtcpclient.obj.enc 26KB
TCPClient.exe.recipe 273B
mbasciislave.obj 19KB
vc142.pdb 9.28MB
mbtcp.obj 7KB
mbcommon.obj 19KB
mbrtuslave.obj 18KB
mbasciimaster.obj 24KB
vc142.idb 1.34MB
TCPClient.pch 55.56MB
pch.obj 735KB
mbtcpclient.obj 26KB
TCPClientDlg.obj 496KB
TCPClient.res 68KB
TCPClient.rc 11KB
TCPClient.h 496B
TCPClient.aps 107KB
TCPApplication.sln 2KB
共 75 条
- 1
资源评论
- 一线贫民_2022-03-02依赖文件,没有一起打包,无法使用,清大家不要下载
- 圆道2023-11-03不错!缺少的文件去下大佬的git库 #运行顺畅
- kaney41152021-04-05缺少文件,浪费积分了
foxclever
- 粉丝: 2499
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功