LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   TCP Client Socket C Programming (Send/Receive) (https://www.linuxquestions.org/questions/programming-9/tcp-client-socket-c-programming-send-receive-4175718780/)

nather 11-14-2022 07:34 PM

TCP Client Socket C Programming (Send/Receive)
 
Hi All,

I have sent data and received data from the server. I'm using TCP Client Socket C Programming I have connected the server and I send the data but the server can't receive it (Note: Server successfully connected). This is my data
example: '050560000000001414103037323330......' (1032 bytes and Decimal 514). how I can read this data and send it to the server socket using the C program? Please advice.

my doubts
1. How to assign these big bytes variables statically using the c program.
2. How do I read this big byte and send them to the socket send method?

Below mentioned my code. Please find it.

(Note: Server successfully connected but server not received and not sent)

Code:

int main(int argc, char *argv[])
{
    int Socket, read_size;
    struct sockaddr_in server;
    char SendToServer[255] = "050560000000001414103037323330....";
    char server_reply[255] = {0};
    //Create socket
    Socket = SocketCreate();
    if(Socket == -1)
    {
        printf("Could not create socket\n");
        return 1;
    }
    printf("Socket is created\n");
    //Connect to remote server
    if (SocketConnect(Socket) < 0)
    {
        perror("connect failed.\n");
        return 1;
    }
    printf("Sucessfully conected with server\n");
    gets(SendToServer);
    //Send data to the server
    SocketSend(Socket, SendToServer, strlen(SendToServer));
    //Received the data from the server
    read_size = SocketReceive(Socket, server_reply, 200);
    printf("Server Response : %s\n\n",server_reply);
    close(Socket);
    shutdown(Socket,0);
    shutdown(Socket,1);
    shutdown(Socket,2);
    return 0;
}
short SocketCreate(void)
{
    short Socket;
    printf("Create the socket\n");
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    return Socket;
}

int SocketConnect(int Socket)
{
    int iRetval=-1;
    int ServerPort = 90190;
    struct sockaddr_in remote= {0};
    remote.sin_addr.s_addr = inet_addr("127.0.0.1");
    remote.sin_family = AF_INET;
    remote.sin_port = htons(ServerPort);
    iRetval = connect(Socket,(struct sockaddr *)&remote,sizeof(struct sockaddr_in));
    return iRetval;
}

int SocketSend(int Socket,char* Rqst,short lenRqst)
{
    int shortRetval = -1;
    struct timeval tv;
    tv.tv_sec = 20;  /* 20 Secs Timeout */
    tv.tv_usec = 0;
    if(setsockopt(Socket,SOL_SOCKET,SO_SNDTIMEO,(char *)&tv,sizeof(tv)) < 0)
    {
        printf("Time Out\n");
        return -1;
    }
    shortRetval = send(Socket, Rqst, lenRqst, 0);
    return shortRetval;
}

int SocketReceive(int Socket,char* Rsp,short RvcSize)
{
    int shortRetval = -1;
    struct timeval tv;
    tv.tv_sec = 20;  /* 20 Secs Timeout */
    tv.tv_usec = 0;
    if(setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(tv)) < 0)
    {
        printf("Time Out\n");
        return -1;
    }
    shortRetval = recv(Socket, Rsp, RvcSize, 0);
    printf("Response %s\n",Rsp);
    return shortRetval;
}


NevemTeve 11-15-2022 02:33 AM

Verify your program via running `netcat` as server:
Code:

nc -l -p 90190

nather 11-15-2022 09:21 AM

Quote:

Originally Posted by NevemTeve (Post 6392431)
Verify your program via running `netcat` as server:
Code:

nc -l -p 90190

Hi NevemTeve,

Thanks for your response.

I'm using POS Linux terminal and my server is ASTREX.

NevemTeve 11-15-2022 10:35 AM

That's commendable. I suggest you test your program with `netcat` running as server: `nc -l -p 90190`

nather 11-17-2022 10:27 AM

Quote:

Originally Posted by NevemTeve (Post 6392490)
That's commendable. I suggest you test your program with `netcat` running as server: `nc -l -p 90190`

Thank you for your suggestion brother!

chrism01 11-17-2022 09:07 PM

FYI, this is a popular guide to Network Programming http://www.cs.columbia.edu/~danr/cou...1-sockhelp.pdf

NevemTeve 11-17-2022 11:41 PM

Now that you mention it 90190 might be above the maximum (65535) -- most likely it is interpreted as 24654 (mod 65536) or as invalid value.

pan64 11-18-2022 01:19 AM

Quote:

Originally Posted by NevemTeve (Post 6392957)
Now that you mention it 90190 might be above the maximum (65535) -- most likely it is interpreted as 24654 (mod 65536) or as invalid value.

in that case the connect should report an error and also the code should check if that connect was successful.

NevemTeve 11-18-2022 07:54 AM

1 Attachment(s)
If you add the missing includes to the program it sort of works.
Terminal 1:
Code:

$ netcat -l -p 90190
Terminal 2:
Code:

$ echo 'Ezt a kort a pivel szamold ki' | ./nather
Create the socket
Socket is created
Sucessfully conected with server
Response
Server Response :

Terminal 1:
Code:

$ netcat -l -p 90190
Ezt a kort a pivel szamold kiprojects$

Mind you, gets is not to be used. Especially not with binary data.

nather 11-18-2022 09:50 AM

Quote:

Originally Posted by NevemTeve (Post 6393005)
If you add the missing includes to the program it sort of works.
Terminal 1:
Code:

$ netcat -l -p 90190
Terminal 2:
Code:

$ echo 'Ezt a kort a pivel szamold ki' | ./nather
Create the socket
Socket is created
Sucessfully conected with server
Response
Server Response :

Terminal 1:
Code:

$ netcat -l -p 90190
Ezt a kort a pivel szamold kiprojects$

Mind you, gets is not to be used. Especially not with binary data.

Thank you for your suggestion I will check and let you know.


All times are GMT -5. The time now is 10:05 PM.