Menu

Problems sending text using sockets

C.F.
2009-02-17
2012-09-26
  • C.F.

    C.F. - 2009-02-17

    Hello, I'm trying to send text using TCP sockets but I don't know what I am doing wrong, I think that I've tried everything. How can I change the code so I can send and receive the text "Hello World"?

    include <stdio.h>

    include <stdlib.h>

    include <windows.h>

    include <winsock.h>

    define PORT 6000

    define IP "127.0.0.1"

    int main()
    {
    WORD winsock; // Winsock version
    WSADATA datos; // Data to initialize winsock
    SOCKET ToListen; // With this socket we accept connections
    SOCKET communication; // Socket to send and receive data
    struct sockaddr_in server; // Data for connection
    int tam_sockaddr=sizeof(struct sockaddr);
    int num_sent=1;
    int num_received;

    // Initialize winsock
    winsock=MAKEWORD(1,1);
    if(WSAStartup(winsock, &datos))
    printf("Error: Problems when initialising winsock\n");
    else
    {
    // Create socket.
    ToListen=socket(AF_INET, SOCK_STREAM, 0);
    if(ToListen==-1)
    printf("Error: Problems when creating the socket\n");
    else
    {
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
    server.sin_addr.s_addr=inet_addr(IP); // IP in long format

    // Associate ip & PORT to the socket
    if((bind(ToListen, (struct sockaddr *)&server, sizeof(server)))==-1)
    printf("Error: Problems when associating the PORT & IP to the socket\n");
    else
    {
    listen(ToListen, 1);

    // Accept connections
    printf("Waiting for connections... \n");
    communication=accept(ToListen, (struct sockaddr *)&server, &tam_sockaddr);

    // We aren't going to accept more connections so we'll close the socket socket ToListen
    close(ToListen);

    // We send the number and then we'll wait to receive the number from the client
    printf("Sending number... \n");
    send (ToListen, (char )&num_sent, sizeof(num_sent), 0);
    printf("Receiving number... \n");
    recv (ToListen, (char
    )&num_received, sizeof(num_received), 0);

    printf("Number sent: %d \n", num_sent);
    printf("Received number: %d \n", num_received);

    // Close socket
    closesocket(communication);
    }
    }
    // Close winsock library
    WSACleanup();
    }

    system("PAUSE");
    return (EXIT_SUCCESS);
    }

     
    • cpns

      cpns - 2009-02-27

      But your code did not attempt to send any text, so there was no example of what your were trying and failing to do. You ought to post the code you are asking about, not a different example of something you can do. An example of what you tried would give us a better understanding of what you were asking.

      You also have to consider that when we see code of the quality and level of that which you posted, using sockets and such, that we don't really expect the author of it to stumped by the simple act sending a string! So it seemed that there was more to the question that there actually was, hence the apparent irrelevance of the counter-questions and requests.

      Then you made the point about adding "libwsock32.a" as if that had some bearing on the problem. For that to have been relevant, it seemed reasonable that you were having issues linking the code. If the code compiled and linked (which was not clear) than what libraries you linked were irrelevant.

      There are plenty of smart people here, if you cannot get an answer to a question that seems obvious to you, you have to consider if the problem is the way you asked it. In view of the way you responded, you may not get many willing volunteers in future. That said, I suspect that you spent less time figuring out the solution that you did composing the question in the first place. So I wonder why you posted at all?

      The supposition that no one knew the answer is ridiculous. The solution was obvious, it was the way you posted the question that was flawed.

       
    • C.F.

      C.F. - 2009-02-19

      I forgot to mention that I added the library "libwsock32.a" to the project correctly.

       
    • cpns

      cpns - 2009-02-20

      You have not provided much information, leaving us with no choice but to wade through the code, or attempt to compile it. Which I am not about to do.

      Does it compile? Does it crash? What does it do!?

      > I forgot to mention that I added the library
      > "libwsock32.a" to the project correctly.

      No. The way you provide that information is to simply post the build log as requested in the "PLEASE READ BEFORE POSTING A QUESTION" thread. That way we know exactly how your project is configured, and we don't have to just 'believe' you know what you are doing.

      Clifford

       
    • C.F.

      C.F. - 2009-02-20

      Of course I read it.. the problem is that I don't get errors. The problem is that I can send numbers
      but I don't know how to send text...

      Anyways.. I solved it :)

      char *text = "abcdef";
      send (ToListen, text, strlen(text)+1, 0);

      Thanks for the time

       
      • Wayne Keen

        Wayne Keen - 2009-02-20

        "the problem is that I don't get errors"

        The purpose of posting the log is NOT always about the errors.

        In remote debugging, what we need to know is how you set up things so as close to
        possible how your code is configured to compile, what libraries are linked, where
        things are, there is a world of information there besides the error message.

        Here's the point. When you ask for help, and you are asked to provide certain
        information, DO NO start saying things like "well, I don't get errors, so I
        don't need to tell him that". As you posts here demonstrated, you don't
        know enough about the process to make such judgements.

        Wayne

         
    • C.F.

      C.F. - 2009-02-27

      The problem was solved Wayne!! I don't understand what you pretend when posting once the problem is solved if you didn't help in anything. Sorry... but it would be easier and fast if you don't know the answer: "Sorry but NOBODY in this forum knows the answer. Please, ask it anywhere."

      The problem was about sockets, if it's supposed the Dev-C++ to be configured correctly (as it is) and it can send numbers but not text, the problem is in the code. So I was asking for help. But I'll take it in mind to say when someone is asking for help and I have NO IDEA to say the same that you recently posted: "As you posts here demonstrated, you don't know enough about the process to make such judgements. "

      If you think you can do it better, post a code (or pseudocode) to show what you can do (and anybody could learn also from you, included me). I'm sure that it helps more than that poor post with no information about sockets.

      Regards.

       
      • Wayne Keen

        Wayne Keen - 2009-02-27

        Actually, this forum is a community, and the message was
        to others that read this forum that the point of posting
        the log is NOT just to see the error messages.

        Wayne

         
    • C.F.

      C.F. - 2009-02-27

      What error messages? who talked about them? I was asking for that little information and nothing else. If you can send numbers but not text, it's obvious that I'm not getting any error messages. Anyways, it's not good to start arguing about that. The problem is solved and I also posted the solution for others.

      Regards.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.