资源简介
windows网络编程serverandclient猜拳游戏
代码片段和文件信息
#include “stdafx.h“
#include
#include
#pragma comment(lib“WS2_32.lib“)
#define BUF_SIZE 1024
void main(void)
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int ClientAddrLen;
int Port = 5150;
int Ret;
char DataBuffer[ BUF_SIZE];
// Initialize Winsock version 2.2
if ((Ret = WSAStartup(MAKEWORD(22) &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.
printf(“WSAStartup failed with error %d\n“ Ret);
return;
}
printf(“successful 1\n“);
// Create a new socket to listening for client connections.
if ((ListeningSocket = socket(AF_INET SOCK_STREAM IPPROTO_TCP))
== INVALID_SOCKET)
{
printf(“socket failed with error %d\n“ WSAGetLastError());
WSACleanup();
return;
}
printf(“successful 2\n“);
// Setup a SOCKADDR_IN structure that will tell bind that we
// want to listen for connections on all interfaces using port
// 5150. Notice how we convert the Port variable from host byte
// order to network byte order.
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Associate the address information with the socket using bind.
printf(“successful 3\n“);
if (bind(ListeningSocket (SOCKADDR *)&ServerAddr sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf(“bind failed with error %d\n“ WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}
printf(“successful 4\n“);
// Listen for client connections. We used a backlog of 5 which is
// normal for many applications.
if (listen(ListeningSocket 5) == SOCKET_ERROR)
{
printf(“listen failed with error %d\n“ WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}
printf(“We are awaiting a connection on port %d.\n“ Port);
// Accept a new connection when one arrives.
ClientAddrLen=sizeof(ServerAddr);
printf(“successful 5\n“);
if ((NewConnection = accept(ListeningSocket (SOCKADDR *) &ClientAddr
&ClientAddrLen)) == INVALID_SOCKET)
{
printf(“accept failed with error %d\n“ WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}
printf(“We successfully got a connection from %s:%d.\n“
inet_ntoa(ClientAddr.sin_addr) ntohs(ClientAddr.sin_port));
// At this point you can do two things with these sockets. Await
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7079 2014-10-14 18:55 guess\w.cpp
文件 3758 2014-10-14 18:55 guess\ws.cpp
目录 0 2014-11-03 20:04 guess\
- 上一篇:将图片二进制流存储到数据库中
- 下一篇:gps模拟信号软件
相关资源
- 淘宝上的微信小程序和小游戏视频教
- QT编写的Socket工具,TCP 以及 UDP 可建立
- JS打地鼠小游戏源码
- Qt TCP 聊天软件
- Spring+Netty+WebSocket
- linux环境下基于socket通信的ftp系统
- 基于CAsyncSocket类的网络编程-客户端
- 使用CAsyncSocket完成的客户端和服务端
- iOS socket连接打印机打印小票
- gameServer
- Windows网络编程第2版-源代码
- 基于labview的小游戏读心术
- [delphi]简单HTTP服务器架设源码
- tcpClientAndServerTools v1.0.0.rar
- ArcGIS Server 10.3.zip
- 实现了匹配机制同时可以进行数据交
- 1.12.2minecraftserver.txt
- ExchangeServer2019KEY.rar
- ge VC opc server
- 示范了Unix和Linux下如何利用Raw Socket构
- OPC 基金组织最新Opc Server源码
- 通讯服务器,带Socket功能,操作串口
- vue封装websocket.zip
- 目前最高性能的SocketAsyncEventArgs
- arcgis server 10.2.2 许可文件
- arcgis10.3.1正式版完整版包含desktop+se
- 幸运转盘微信小游戏源代码
- Netzob官网的server以及client
- web应用小游戏
- 成语接龙 游戏 小游戏
评论
共有 条评论