在这种情况下,我如何使用char指针?(Windows中的SOCKET编程)

时间:2016-02-05 05:58:45

标签: c sockets winsock tcp-ip winsock2

注意代码

SERVER.c

char* message = "hel";
send(clientsock, &message, 3, 0); 

CLIENT.c

char** message = NULL;
messageLen = recv(clientsock, **message, 2, 0); 

我的计划是在char * message =“hel”中的SERVER.c source发送到char **消息中的CLIENT.c源。但它不起作用,我对我的计划做了什么?

Server.c完整来源

#pragma comment(lib, "ws2_32.lib")
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <WinSock2.h>
#include <stdlib.h>

#define PORT 1234
#define BACKLOG 10



int main(int argc, char * argv[]) {

SOCKET sock, clientsock;
WSADATA wasData;
struct sockaddr_in sockinfo, clientinfo;
int clientsockSize;
char* message = "hel";


if (WSAStartup(MAKEWORD(2, 2), &wasData) != 0) {  //int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
    return 0;
}

sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // socket( AF_INET , type , protocol)

if (sock == INVALID_SOCKET) {
    return 1;
}

sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(PORT); // 
sockinfo.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(sock, (SOCKADDR*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR) { // int bind (sock , (SOCKADDR*)&sockinfo , sizeof(sockinfo)) if true : return 0, else -1
    return 2;
}

if (listen(sock, BACKLOG) == SOCKET_ERROR) {  // Int listen ( SOCKET , backlog )
    return 3;
}


clientsockSize = sizeof(clientinfo);

printf("접속 대기중...\n");

clientsock = accept(sock, (SOCKADDR*)&clientinfo, &clientsockSize); 


if (clientsock == INVALID_SOCKET) {
    return 4;
}

printf("Send your message >> ");

send(clientsock, &message, sizeof(message), 0);  

closesocket(sock);
closesocket(clientsock);

WSACleanup();

return -1;

}

CLIENT.c完整来源

#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma comment(lib, "ws2_32.lib")

#include <stdio.h>
#include <WinSock2.h>


int main(int argc, char* argv[]) {

SOCKET clientsock;
WSADATA wasData;
struct sockaddr_in sockinfo;

char** message = NULL;

int messageLen;

if (argc != 3) {
    printf("Argument is not sufficient!\nInputed Argument >> %d", argc);
    exit(1);
}

if (WSAStartup(MAKEWORD(2, 2), &wasData) != 0) {
    printf("Error >> Init WSAStartup(..)\n");
}

clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientsock == INVALID_SOCKET)
    printf("Error >> socket(..)\n");
memset(&sockinfo, 0, sizeof(sockinfo)); // 구조체 초기화

sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(atoi(argv[2]));
sockinfo.sin_addr.s_addr = inet_addr(argv[1]);

if (connect(clientsock, (SOCKADDR*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
    printf("Error >> connect(..)\n");

messageLen = recv(clientsock, **message, sizeof(message) - 1, 0);
if (messageLen == -1)
    printf("Error >> recv(..)\n");

printf("Server say >> %s", message);
closesocket(clientsock);
WSACleanup();

}

1 个答案:

答案 0 :(得分:1)

客户端

您必须使用已分配的缓冲区从套接字接收字符。

您正在使用指向bo分配内存的指针

char** message = NULL;

您必须使用

char message[MAX_MSG_DIM] = {0};

char *message = malloc(MAX_MSG_DIM);

请注意,recv在收到所有角色时都不会返回,但仅限于可用的角色。这意味着从客户端recv接收整个消息可以被多次调用。您必须对收到的字符进行计数,以确定是否已收到整条消息。

服务器端

向您撰写的客户发送消息

send(clientsock, &message, sizeof(message), 0);

这是错的。所有你必须传递消息的指针,其次大小不是检索字符串长度的正确方法。您必须使用strlen来执行此操作。

send(clientsock, message, strlen(message)+1, 0);

我写了strlen(message)+1,因为在您的客户端,您没有在收到的缓冲区上添加NULL终结符。然后你必须从服务器端发送它。