SOCKET
来源:互联网 发布:游戏美工是什么专业 编辑:程序博客网 时间:2024/06/06 03:01
一个服务器,若干个客户,会怎么样?
服务器端
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 1024
#pragma comment(lib, "Ws2_32.lib")
SOCKET GLOBAL[100];
DWORD WINAPI handle_a_connect(LPVOID aa)
{
SOCKET ClientSocket = *(SOCKET*)aa;
SOCKADDR_IN sockAddr;
int iLen = sizeof(sockAddr);
//获取local ip and port
ZeroMemory(&sockAddr, sizeof(sockAddr));
getpeername(ClientSocket, (struct sockaddr *)&sockAddr, &iLen);//得到远程IP地址和端口号
char *nSourceIP = inet_ntoa(sockAddr.sin_addr);
int nSourcePort = sockAddr.sin_port;
//获取remote ip and port
ZeroMemory(&sockAddr, sizeof(sockAddr));
getsockname(ClientSocket, (struct sockaddr *)&sockAddr, &iLen);//得到本地的IP地址和端口号
char *nDestIP = inet_ntoa(sockAddr.sin_addr);//IP
int nDestPort = sockAddr.sin_port;
//端口号
char recvbuf[DEFAULT_BUFLEN];
int iResult;
do {
iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFLEN, 0);
if (iResult > 0) {
printf("Server received: %s from %s:%d(i am %s:%d)\n", recvbuf, nSourceIP, nSourcePort, nDestIP, nDestPort);
// Echo the buffer back to the sender
int iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
while (1);
return 1;
}
printf("Server send: %s from %s:%d(i am %s:%d)\n", recvbuf, nSourceIP, nSourcePort, nDestIP, nDestPort);
}
else if (iResult == 0) {
printf("Connection closing...\n");
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
while (1);
return 1;
}
closesocket(ClientSocket);
}
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
while (1);
return 1;
}
} while (iResult > 0);
return 0;
}
int main(void)
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
//int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
while (1);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
while (1);
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
while (1);
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
while (1);
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
while (1);
return 1;
}
// Accept a client socket
int i = 0;
while (1) {
ClientSocket = accept(ListenSocket, NULL, NULL);
i = (i + 1) % 100;
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
while (1);
return 1;
}
GLOBAL[i] = ClientSocket;
HANDLE hand = CreateThread(NULL, 0, handle_a_connect, &GLOBAL[i], 0, NULL);
}
// No longer need server socket
closesocket(ListenSocket);
WSACleanup();
while (1);
return 0;
}
客户机端
#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 1024
#pragma comment(lib, "Ws2_32.lib")
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char sendbuf[DEFAULT_BUFLEN];
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Validate the parameters
if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
while (1);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
while (1);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
while (1);
return 1;
}
printf("argv[0] is :%s, argv[1] is: %s\n", argv[0], argv[1]);
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
while (1);
return 1;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("socket failed with error\n");
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
printf("i'm going to break\n");
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
while (1);
return 1;
}
// Send an initial buffer
scanf_s(" %s", sendbuf, DEFAULT_BUFLEN);
printf("strlen(sendbuf) is %d, sendbuf is %s\n", strlen(sendbuf), sendbuf);
//sendbuf[strlen(sendbuf)] = '\0';
while (strcmp("!", sendbuf) != 0) {
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf)+1, 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
while (1);
return 1;
}
printf("client Sent: %s\n", sendbuf);
// shutdown the connection since no more data will be sent
/*
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
while (1);
return 1;
}
*/
// Receive until the peer closes the connection
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Client received: %s\n", recvbuf);
scanf_s("%s", sendbuf, DEFAULT_BUFLEN);
//sendbuf[strlen(sendbuf)] = '\0';
}
// cleanup
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
while (1);
return 1;
}
closesocket(ConnectSocket);
WSACleanup();
while (1);
return 0;
}
- socket
- socket
- Socket
- Socket
- Socket
- Socket
- Socket
- Socket
- socket
- Socket
- Socket
- Socket
- Socket
- socket
- socket
- socket
- socket
- socket
- (转)在用户空间发生中断时,上下文切换的过程
- ios
- 朴素贝叶斯例题记录
- logstash _source host:0.0.0.0 host不显示主机名
- sqlyog注册码激活
- SOCKET
- 深入了解 Oracle Flex ASM 及其优点
- 淡淡
- singleNumber
- Excel学习更新中
- 简单迭代法求解方程举例
- SpingMVC异常02
- 高级SQL注入:混淆和绕过
- ACM日记_17.3.14——快速幂取模