无法通过c套接字编程连接到套接字服务器

时间:2016-02-24 06:26:26

标签: c sockets

我已经搜索了很多turorial并尝试多次测试,但我的客户端应用程序仍然无法连接到服务器应用程序,我现在正在学习c socket,这是我用c编写的第一个应用程序,我已尝试使用c#socket其他套接字应用程序,它仍然有效,我不知道问题来自我的系统(即时使用窗口7)或库本身,我也试图关闭防火墙,但问题仍然存在,请给我一些建议,这是我从互联网上的教程中借来的代码:

TCP服务器:

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>

#pragma comment(lib, "Ws2_32.lib")

#define MAXPENDING 5

typedef struct sockaddr_in SockAdd;

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

    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( 8888 );

    //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());
    }

    puts("Connection accepted");

    closesocket(s);
    WSACleanup();

    return 0;
}

TCP客户端:

#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
#include <string.h>

#pragma comment(lib, "Ws2_32.lib")

#define RECBUFFERSIZE 32
typedef struct sockaddr_in SockAdd;

int main(int argc, char* argv[]){
    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;

    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");


    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected");

    return 0;
}

关注我的测试图片: Error when connecting server socket

感谢大家阅读我的帖子。

我从winsock-socket-programming-tutorial借用了代码。

特别感谢kushdilip,解决方案是改变&#34; server.sin_addr.s_addr = INADDR_ANY&#34;进入&#34; server.sin_addr.s_addr = inet_addr(&#34; 127.0.0.1&#34;)&#34;,word up!

0 个答案:

没有答案