Winsock Server Code
以下代码来自:https://msdn.microsoft.com/en-us/library/windows/desktop/ms737593(v=vs.85).aspx
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl 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);
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();
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();
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();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
// Echo the buffer back to the sender
iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
Winsock Server Code的更多相关文章
- Socket tips: UDP Echo service - Server code
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/soc ...
- web socket server code, 调用 shell exec child_process
var child_process = require('child_process'); var ws = require("nodejs-websocket"); consol ...
- UDP server Code
Code Example: The following programs demonstrate the use of getaddrinfo(), gai_strerror(), freeaddri ...
- 01MySQL内核分析-The Skeleton of the Server Code
摘要 这个官方文档一段对MySQL内核分析的一个向导.是对MySQL一条insert语句写入到MySQL数据库的分析. 但是,对于MySQL 5.7版本来说,基本上都是写入到innodb引擎.但也还是 ...
- Winsock Client Code
以下代码来自MSDN:https://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx #define W ...
- SQL Server Code tips (持续更新)
1. 表存在,查询语句也能执行,但是表名下面总是有条红线,说对象名无效 CTRL + SHIFT +R 刷新本地缓存就可以了 2. IDE (Integrated Development Envi ...
- Winsock网络编程笔记(2)----基于TCP的server和client
今天抽空看了一些简单的东西,主要是对服务器server和客户端client的简单实现. 面向连接的server和client,其工作流程如下图所示: 服务器和客户端将按照这个流程就行开发..(个人觉得 ...
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...
- Code First Migrations: Making __MigrationHistory not a system table
https://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system- ...
随机推荐
- Excel文件读写操作1(xls)
一.Python-Excel常用的库:xlrd(read).xlwt(write).xlutils.openpyxl 1.xlrd 读取Excel文件,支持 .xls 和 .xlsx 格式. 2.x ...
- vue 列表过滤和排序
<body> <div id="root"> <h1>员工列表</h1> <input type="text&quo ...
- Linux系统inodes资源耗尽问题
1 inodes介绍 Linux系统下文件数据储存在"块"中,文件的元信息,例如文件的创建者.文件的创建日期.文件的大小等.这种储存文件元信息的区域就叫做inode,中文译名为&q ...
- 从OC角度思考OKR的底层逻辑
原创不易,求分享.求一键三连 扩展阅读:什么是OKR OC:Organization Cultrue即组织文化,标题用OC纯粹为了装逼... 自从接受公司文化建设工作后,思维发生了很大的变化,文化, ...
- SpringBoot从0到0.7——第三天
SpringBoot从0到0.7--第三天 今天学习整合JDBC,连接数据库的增删改查,写出来容易,理解原理读懂代码才是主要的. 首先创建项目,勾选上一下模块 在application.yml添加 s ...
- KD-Tree及希尔伯特空间填充曲线的应用
引言 我们可能会有这样的一种需求,像是打车软件中呼叫附近的车来接送自己,或者是在qq中查看附近的人.我们都需要知道距离自己一定范围内的其它目标的集合.如果将上面举例的功能抽象出来,就是要实现以某个点为 ...
- Python数据分析--Numpy常用函数介绍(2)
摘要:本篇我们将以分析历史股价为例,介绍怎样从文件中载入数据,以及怎样使用NumPy的基本数学和统计分析函数.学习读写文件的方法,并尝试函数式编程和NumPy线性代数运算,来学习NumPy的常用函数. ...
- JavaScript Object学习笔记一
Object.assign(target, source1, source2, ...)//用于对象的复制合并(同名属性后覆盖前)或拷贝(拷贝自身可枚举属性,不拷贝继承属性或不可枚举属性),将sour ...
- 互联网公司目标管理OKR和绩效考核的误区
最近看了一篇关于「谷歌放弃OKR,转向全新的GRAD系统」的文章,我转到了研发效能DevOps的微信群里,结果引起了大家热烈的讨论,正好我们也在使用 OKR,所以也来谈谈我的理解以及我们应用起来的实际 ...
- GIF录制工具下载
GIF录制工具(GifCam)下载地址: https://files.cnblogs.com/files/blogs/747754/GifCam.rar