0
已解决
王牌工作室官方
新手光能
新手光能
有空就连载
王牌工作室官方在2022-06-27 15:19:58追加了内容
一,基本数据发送
第1课 : 一对一连接 - 服 务 器
先在编译设置里添加-lwsock32
再输入基本头文件和常量
#include <WINSOCK2.H>
#include <stdio.h>
#define PORT 5150
#define MSGSIZE 1024
那个5150可以改,但不要改成80,也不要和别的程序冲突(比如和其他联机程序相同),那是端口号
写上main函数,开始
初始化变量
WSADATA wsaData;
SOCKET sListen;
SOCKET sClient;
SOCKADDR_IN local;
SOCKADDR_IN client;
char szMessage[MSGSIZE];
int ret;
int iaddrSize = sizeof(SOCKADDR_IN);
注意wsaData,szMessage和sClient变量,这几个要用到
来初始化一下
WSAStartup(0x0202, &wsaData);
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
local.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sListen, (struct sockaddr *) &local, sizeof(SOCKADDR_IN));
listen(sListen, 1);
现在可以连接客户端了
sClient = accept(sListen, (struct sockaddr *) &client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr),ntohs(client.sin_port));//本行可选
不断接受消息
while(1)
{
ret = recv(sClient, szMessage, MSGSIZE, 0);
szMessage[ret] = '\0';
printf("Received [%d bytes]: '%s'\n", ret, szMessage);
}
本课全部代码:
#include <WINSOCK2.H>
#include <stdio.h>
#define PORT 5150
#define MSGSIZE 1024
int main()
{
WSADATA wsaData;
SOCKET sListen;
SOCKET sClient;
SOCKADDR_IN local;
SOCKADDR_IN client;
char szMessage[MSGSIZE];
int ret;
int iaddrSize = sizeof(SOCKADDR_IN);
WSAStartup(0x0202, &wsaData);
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
local.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sListen, (struct sockaddr *) &local, sizeof(SOCKADDR_IN));
listen(sListen, 1);
sClient = accept(sListen, (struct sockaddr *) &client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
while (TRUE)
{
ret = recv(sClient, szMessage, MSGSIZE, 0);
szMessage[ret] = '\0';
printf("Received [%d bytes]: '%s'\n", ret, szMessage);
}
return 0;
}
王牌工作室官方在2022-06-27 15:28:10追加了内容
第1课 : 一对一连接 - 客 户 端
头部:
#include <WINSOCK2.H>
#include <stdio.h>
#define SERVER_ADDRESS "127.0.0.1"
#define PORT 5150
#define MSGSIZE 1024
一样,PORT可以改,但必须和服 务 器的PORT一样
SERVER_ADDRESS也可以改,这是服 务 器的IP地址(必须连接在同一个网络)
变量
WSADATA wsaData;
SOCKET sClient;
SOCKADDR_IN server;
char szMessage[MSGSIZE];
int ret;
初始化
WSAStartup(0x0202, &wsaData);
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&server, 0, sizeof(SOCKADDR_IN));
server.sin_family = PF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
连接
connect(sClient, (struct sockaddr *) &server, sizeof(SOCKADDR_IN));
不断发送
while(1)
{
printf("Send:");
gets(szMessage);
if(strcmp(szMessage,"**exit**")==0) break;
send(sClient, szMessage, strlen(szMessage), 0);
}
清理
closesocket(sClient);
WSACleanup();
这两课的代码含义在以后会讲,现在只要知道就行了
王牌工作室官方在2022-06-27 15:29:22追加了内容
完整代码
#include <WINSOCK2.H>
#include <stdio.h>
#define SERVER_ADDRESS "127.0.0.1"
#define PORT 5150
#define MSGSIZE 1024
int main()
{
WSADATA wsaData;
SOCKET sClient;
SOCKADDR_IN server;
char szMessage[MSGSIZE];
int ret;
WSAStartup(0x0202, &wsaData);
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&server, 0, sizeof(SOCKADDR_IN));
server.sin_family = PF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
connect(sClient, (struct sockaddr *) &server, sizeof(SOCKADDR_IN));
while(1)
{
printf("Send:");
gets(szMessage);
if(strcmp(szMessage,"**exit**")==0) break;
send(sClient, szMessage, strlen(szMessage), 0);
}
closesocket(sClient);
WSACleanup();
return 0;
}