编辑:向下滚动以查看更新的代码。
我想用 C 语言为 Minecraft 构建一个假播放器。
我的 Minecraft 服务器 (bukkit) 在我的本地 IP 192.168.1.141 上侦听端口 25565
当我启动我的应用程序时,(当它通过套接字发送数据时)bukkit 服务器说:“192.168.1.141:xxxxx 失去连接”,并且
我的申请告诉我:
socked created
socked connected
to send data = (smilie)kekos91;192.168.1.141:25565
to send length = 27
send !
response data = (a strange symbol)
response length = 1
socket disconnected
也许这是因为 char-set ?有人知道为什么吗?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winsock2.h>
int main()
{
char pseudo[] = "kekos91;192.168.1.141:25565";
int pseudoLen = strlen(pseudo);
char *packet = NULL;
packet = (char*)malloc(3 + ((pseudoLen)*sizeof(char)*2));
if(packet == NULL)
{
return -1;
}
memset(packet, '\0', sizeof(packet));
packet[0] = (char)0x02;
strcat(packet, pseudo);
int pLenght = strlen(packet);
char handshake[200];
HANDLE hConsole;
WSADATA wsaData;
int iResult = 0;
SOCKET serverSocket;
sockaddr_in serverInfos;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
printf("WSAStartup() failed with error: %d\n", iResult);
return -1;
}
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
printf("socket function failed with error: %i\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("socket created\n");
serverInfos.sin_family = AF_INET;
serverInfos.sin_addr.s_addr = inet_addr("192.168.1.141");
serverInfos.sin_port = htons(25565);
iResult = connect(serverSocket, (SOCKADDR *)&serverInfos, sizeof(serverInfos));
if (iResult == SOCKET_ERROR)
{
printf("connect function failed with error: %i\n", WSAGetLastError());
iResult = closesocket(serverSocket);
if (iResult == SOCKET_ERROR)
{
printf("closesocket function failed with error: %i\n", WSAGetLastError());
}
WSACleanup();
return -1;
}
printf("socket connected\n");
printf("to send data = %s\n", packet);
printf("to send length = %i\n", pseudoLen);
Sleep(500); // after this point, the connection is loose :(
if(send(serverSocket, (char*)packet , pLenght, 0) == SOCKET_ERROR)
{
printf("send function failed with error: %i\n", WSAGetLastError());
iResult = closesocket(serverSocket);
if (iResult == SOCKET_ERROR)
{
printf("closesocket function failed with error: %i\n", WSAGetLastError());
}
WSACleanup();
return -1;
}
free(packet);
printf("send !\n");
if(recv(serverSocket, (char*)&handshake, 200, 0) == SOCKET_ERROR)
{
printf("recv function failed with error: %i\n", WSAGetLastError());
iResult = closesocket(serverSocket);
if (iResult == SOCKET_ERROR)
{
printf("closesocket function failed with error: %i\n", WSAGetLastError());
}
WSACleanup();
return -1;
}
printf("response data = %s\n", handshake);
printf("response length = %i\n", strlen(handshake));
iResult = closesocket(serverSocket);
if (iResult == SOCKET_ERROR) {
printf("closesocket function failed with error %i\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("socket disconnected.\n");
WSACleanup();
Sleep(5000);
return 0;
}
提前谢谢,请原谅我的英语不好:)
更新 :
多亏了建设性的评论,现在一切正常。这是工作代码,您可以查看我的旧代码发生了什么。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winsock2.h>
int main()
{
int iSocketResult = 0; // it will retrieve sockets return functions, like send / recv / errors
wchar_t wcPseudo[] = L"kekos91;192.168.1.141:25565";
int iPseudoLen = wcslen(wcPseudo);
char *pPacket = NULL;
pPacket = (char*)malloc(3 + ((iPseudoLen)*sizeof(char)*2));
if(pPacket == NULL)
{
return -1;
}
pPacket[0] = 0x02;
*(short*)( pPacket + 1 ) = htons( iPseudoLen );
memcpy( pPacket + 3, wcPseudo, iPseudoLen * sizeof( wchar_t ));
int iPacketLen = 3 + iPseudoLen*sizeof(wchar_t) ;
char *handshake = NULL;
handshake = (char*)malloc(sizeof(char)*200);
if(handshake == NULL)
{
return -1;
}
HANDLE hConsole;
WSADATA wsaData;
SOCKET serverSocket;
sockaddr_in serverInfos;
iSocketResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iSocketResult != NO_ERROR) {
printf("WSAStartup() failed with error: %d\n", iSocketResult);
return -1;
}
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
printf("socket function failed with error: %i\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("socket created\n");
serverInfos.sin_family = AF_INET;
serverInfos.sin_addr.s_addr = inet_addr("192.168.1.141");
serverInfos.sin_port = htons(25565);
iSocketResult = connect(serverSocket, (SOCKADDR *)&serverInfos, sizeof(serverInfos));
if (iSocketResult == SOCKET_ERROR)
{
printf("connect function failed with error: %i\n", WSAGetLastError());
iSocketResult = closesocket(serverSocket);
if (iSocketResult == SOCKET_ERROR)
{
printf("closesocket function failed with error: %i\n", WSAGetLastError());
}
WSACleanup();
return -1;
}
printf("socket connected\n\n");
printf("to send protocol = %#1.2x \nlenght = %i\n", pPacket[0], iPseudoLen);
iSocketResult = send(serverSocket, pPacket , iPacketLen, 0);
if(iSocketResult == SOCKET_ERROR)
{
printf("send function failed with error: %i\n", WSAGetLastError());
iSocketResult = closesocket(serverSocket);
if (iSocketResult == SOCKET_ERROR)
{
printf("closesocket function failed with error: %i\n", WSAGetLastError());
}
WSACleanup();
return -1;
}
free(pPacket);
printf("send %i (bytes) !\n\n",iSocketResult);
iSocketResult = recv(serverSocket, handshake, 50, 0);
if( iSocketResult == SOCKET_ERROR)
{
printf("recv function failed with error: %i\n", WSAGetLastError());
iSocketResult = closesocket(serverSocket);
if (iSocketResult == SOCKET_ERROR)
{
printf("closesocket function failed with error: %i\n", WSAGetLastError());
}
WSACleanup();
return -1;
}
printf("response protocol = %#1.2x\nresponse lenght = %i (bytes)\n\n", handshake[0], iSocketResult);
free(handshake);
iSocketResult = closesocket(serverSocket);
if (iSocketResult == SOCKET_ERROR)
{
printf("closesocket function failed with error %i\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("socket disconnected.\n");
WSACleanup();
Sleep(7000);
return 0;
}
输出控制台现在打印:
socket created
socket connected
to send protocol = 0x02
lenght = 27
send 57 (bytes) !
response protocol = 0x02
response lenght = 37 (bytes)
socket disconnected.
谢谢大家!