Winsock在C中通过TCP发送()

时间:2014-01-10 01:26:40

标签: c winsock

我只是在Winsock中使用send()函数而感到困惑。这段代码实际上是通过TCP发送字符串“Hello”吗?我设法在LabVIEW中建立与TCP客户端的连接,但似乎这个TCP服务器不发送任何东西。

#define DEFAULT_BUFLEN 1024

#include<stdio.h>
#include<winsock2.h>
#include<Ws2tcpip.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#include<stddef.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
int iResult;
char *sendbuf = "Hello";


printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  {
    printf("Failed. Error Code : %d",WSAGetLastError());
    return 1;
  }

printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
  {
    printf("Could not create socket : %d" , WSAGetLastError());
  }

printf("Socket created.\n");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 13000 );

//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
  {
    printf("Bind failed with error code : %d" , WSAGetLastError());
  }

puts("Bind done");

//Listen to incoming connections
listen(s , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");

c = sizeof(struct sockaddr_in);
new_socket = accept(s , (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET)
  {
    printf("accept failed with error code : %d" , WSAGetLastError());
  }

iResult = send( new_socket, sendbuf, (int)strlen(sendbuf), 0 );

if (iResult == SOCKET_ERROR) 
{
    wprintf(L"send failed with error: %d\n", WSAGetLastError());
    closesocket(new_socket);
    WSACleanup();
    return 1;
}
printf("Bytes Sent: %d\n", iResult);

// shutdown the connection since no more data will be sent
iResult = shutdown(new_socket, SD_SEND);
if (iResult == SOCKET_ERROR) 
  {
    wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
    closesocket(new_socket);
    WSACleanup();
    return 1;
  }
}

2 个答案:

答案 0 :(得分:4)

你的代码没有初始化WinSock,没有分配任何SOCKET对象,也没有在调用send()之前在套接字和对等体之间建立连接,所以回答你的问题:

,您的代码不是通过TCP发送字符串。

HOWEVER ,如果填写缺失的部分 - 调用socket()创建TCP套接字,并调用bind()/listen()/accept()与对等方建立TCP连接 - 然后< strong> YES ,您的代码将通过TCP发送字符串。

您需要做更多类似以下的事情。这只是一个建立单个TCP连接的简单示例,然后在将字符串发送到客户端后退出。在实际应用程序中,如果要随着时间的推移为多个客户端连接提供服务,则需要保持服务器套接字打开并持续调用accept(),即使只有一个客户端连接,断开连接并重新连接: / p>

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    int iResult;
    char *sendbuf = "Hello";

    iResult = WSAStartup(MAKEWORD(2, 0), &wsa);
    if (iResult != 0)
    {
        wprintf(L"WinSock startup failed with error: %d\n", iResult);
        return 1;
    }

    server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (server_socket == INVALID_SOCKET) 
    {
        wprintf(L"socket failed with error: %d\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    memset(&server_addr, 0, sizeof(server_addr);
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(some port number here);
    server_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(server_socket, (sockaddr*)&server_addr, sizeof(server_addr)) != 0)
    {
        wprintf(L"bind failed with error: %d\n", WSAGetLastError());
        closesocket(server_socket);
        WSACleanup();
        return 1;
    }

    if (listen(server_socket, 1) != 0)
    {
        wprintf(L"listen failed with error: %d\n", WSAGetLastError());
        closesocket(server_socket);
        WSACleanup();
        return 1;
    }

    iResult = sizeof(client_addr);
    client_socket = accept(server_socket, (sockaddr*)&client_addr, &iResult);
    if (client_socket == SOCKET_ERROR)
    {
        wprintf(L"accept failed with error: %d\n", WSAGetLastError());
        closesocket(server_socket);
        WSACleanup();
        return 1;
    }

    closesocket(server_socket);

    iResult = send(client_socket, sendbuf, strlen(sendbuf), 0);
    if (iResult == SOCKET_ERROR) 
    {
        wprintf(L"send failed with error: %d\n", WSAGetLastError());
        closesocket(client_socket);
        WSACleanup();
        return 1;
    }

    printf("Bytes Sent: %d\n", iResult);

    closesocket(client_socket);
    WSACleanup();
    return 0;
}

更新:根据您更新的代码,试试这个:

#include <stdio.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>

#pragma comment(lib, "ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    int c, iResult;
    char *sendbuf = "Hello";

    printf("Initializing Winsock...\n");

    iResult = WSAStartup(MAKEWORD(2,2), &wsa);
    if (iResult != 0)
    {
        printf("WinSock initialization Failed. Error Code : %d", iResult);
        return 1;
    }

    printf("WinSock Initialized.\n");

    //Create a socket
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET)
    {
        printf("Could not create socket. Error Code : %d" , WSAGetLastError());
        WSACleanup();
        return 1;
    }

    printf("Socket created.\n");

    //Prepare the sockaddr_in structure
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons( 13000 );

    //Bind the listening port
    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
    {
        printf("Bind failed. Error Code : %d", WSAGetLastError());
        closesocket(server_socket);
        WSACleanup();
        return 1;
    }

    printf("Socket bound to port 13000.\n");

    //Listen to incoming connection
    if (listen(server_socket, 1) == SOCKET_ERROR)
    {
        printf("Listen failed. Error Code : %d", WSAGetLastError());
        closesocket(server_socket);
        WSACleanup();
        return 1;
    }

    //Accept an incoming connection
    printf("Waiting for incoming connection...\n");

    c = sizeof(client_addr);
    client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &c);
    if (client_socket == INVALID_SOCKET)
    {
        printf("Accept failed. Error Code : %d", WSAGetLastError());
        closesocket(server_socket);
        WSACleanup();
        return 1;
    }

    printf("Client connected from %s:%hu\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

    //Stop accepting incoming connections
    closesocket(server_socket);

    // Send string to client
    iResult = send(client_socket, sendbuf, strlen(sendbuf), 0);
    if (iResult == SOCKET_ERROR) 
    {
        printf("Send failed. Error Code : %d\n", WSAGetLastError());
        iResult = 1;
    }
    else
    {
        printf("Bytes Sent: %d\n", iResult);
        iResult = 0;
    }

    // shutdown the connection since no more data will be sent
    if (shutdown(client_socket, SD_SEND) == SOCKET_ERROR) 
    {
        printf("Shutdown failed. Error Code : %d\n", WSAGetLastError());
        iResult = 1;
    }

    closesocket(client_socket);
    WSACleanup();

    return iResult;
} 

答案 1 :(得分:1)

不,它不发送字符串“Hello”。即使socket bind / accept / etc连接正常,'send(client_socket,sendbuf,strlen(sendbuf),0);'不发送C字符串。它发送五个字节,而C字符串“Hello”需要六个字节。尝试:

发送(client_socket,sendbuf,1 + strlen(sendbuf),0);

通过搜索'strlen'的源文本,可以找到很高比例的网络C代码问题。 printf(%s ..),并假设TCP传输的消息长于一个字节,占其余部分:)