#include "server0.h" // flexSocket1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <winsock2.h> #include <stdio.h> #include <windows.h> #include <iostream> #pragma comment(lib,"ws2_32.lib") #define LEN 1024 #define OK 1 #define ERROR_R -1 #define FALSE 0 #define TRUE 1 static SOCKET sockets[10]; static int count=0; static int index=0; static int index_main=0; DWORD WINAPI AnswerThreadSend(LPVOID lparam) { printf("-------------------Send AnswerThread be called----------------------\n"); char sendbuf[32] = ""; char sendContent[]="start\n";//必须加\n while(1)//去掉这个就不心跳了 { for(int i=0;i<count;i++) { send(sockets[i], sendContent, strlen(sendContent), 0); } Sleep(1000); } return 0; } bool all_recv(int bytesRecv[]) { for (int i=0;i<count;i++){ if (bytesRecv[i]==SOCKET_ERROR) return false; } return true; } DWORD WINAPI AnswerThreadRecv(LPVOID lparam) { printf("-------------------Recv AnswerThread be called----------------------\n"); int bytesRecv[10]; char recvbuf[10][32] ={"","","", "","","","","","",""};; char sendContent[]="start\n";//必须加\n while(1) { for(int j=0;j<10;j++){ for(int i=0; i<32; i++) { recvbuf[j][i] = '\0'; } } //必须都收到 while(1) { for (int i=0;i<10;i++) bytesRecv[i] = SOCKET_ERROR; for (int index=0;index<10;index++) bytesRecv[index] = recv(sockets[index], recvbuf[index], 32, 0); if (all_recv(bytesRecv))break; } printf("%s\n", recvbuf[0]); printf("%s\n", recvbuf[1]); } return 0; } int main(int argc, char* argv[]) { //初始化WINSOCKET WSADATA wsaData; int iRet = WSAStartup(MAKEWORD(2, 2), &wsaData); if(iRet != NO_ERROR) printf("Error at WSAStartup()\n"); //创建一个Socket SOCKET m_socket; m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(m_socket == INVALID_SOCKET) { printf("Error at socket():%ld\n", WSAGetLastError()); WSACleanup(); return 0; } //绑定socket sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr("127.0.0.1"); service.sin_port = htons(8383); if(bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR) { printf("bind() failed.\n"); closesocket(m_socket); return 0; } else printf("bind OK.\n"); //在socket上侦听 if(listen(m_socket, 20) == SOCKET_ERROR) printf("Error listening on socket.\n"); else printf("Listening ok.\n"); for (int i=0;i<10;i++) { sockets[i]=SOCKET_ERROR; } printf("Waiting for a client to connect...\n"); while(1) { sockets[index_main] = SOCKET_ERROR; while(sockets[index_main] == SOCKET_ERROR) { sockets[index_main]=accept(m_socket, NULL, NULL); index_main++; count=0; for (int i=0;i<10;i++) { if(sockets[i]!=SOCKET_ERROR) count++; } if(index_main>=count) break; } printf("Client Connected.\n"); DWORD dwThreadId; HANDLE hThread; hThread = CreateThread(NULL, NULL, AnswerThreadRecv, NULL, 0, &dwThreadId); if(hThread == NULL) { printf("CreatThread AnswerThread() failed.\n"); } else { printf("CreateThread OK.\n"); } hThread = CreateThread(NULL, NULL, AnswerThreadSend, NULL, 0, &dwThreadId); if(hThread == NULL) { printf("CreatThread AnswerThread() failed.\n"); } else { printf("CreateThread OK.\n"); } } return 0; }
============================================================================================
local socket = require("socket") client = socket.connect("127.0.0.1", 8383) while true do s= client:receive("*l") if s~=nil then client:send("send: client word 111\n") print(s) end end --client:close()