LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2022, 08:34 PM   #1
nather
LQ Newbie
 
Registered: Nov 2022
Posts: 5
Blog Entries: 1

Rep: Reputation: 0
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;
}

Last edited by nather; 11-15-2022 at 12:24 AM.
 
Old 11-15-2022, 03:33 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
Verify your program via running `netcat` as server:
Code:
nc -l -p 90190

Last edited by NevemTeve; 11-15-2022 at 03:36 AM.
 
Old 11-15-2022, 10:21 AM   #3
nather
LQ Newbie
 
Registered: Nov 2022
Posts: 5

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 11-15-2022, 11:35 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
That's commendable. I suggest you test your program with `netcat` running as server: `nc -l -p 90190`
 
Old 11-17-2022, 11:27 AM   #5
nather
LQ Newbie
 
Registered: Nov 2022
Posts: 5

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
That's commendable. I suggest you test your program with `netcat` running as server: `nc -l -p 90190`
Thank you for your suggestion brother!
 
Old 11-17-2022, 10:07 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,118

Rep: Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669
FYI, this is a popular guide to Network Programming http://www.cs.columbia.edu/~danr/cou...1-sockhelp.pdf
 
Old 11-18-2022, 12:41 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
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.
 
Old 11-18-2022, 02:19 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,248

Rep: Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 11-18-2022, 08:54 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
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.
Attached Files
File Type: txt nather.c.txt (2.6 KB, 4 views)

Last edited by NevemTeve; 11-18-2022 at 08:57 AM.
 
Old 11-18-2022, 10:50 AM   #10
nather
LQ Newbie
 
Registered: Nov 2022
Posts: 5

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify UDP receive socket buffer size for an open socket (not at system level, but socket level) barz_83_LQ Linux - Networking 2 11-27-2017 08:56 PM
Connecting client socket to server socket only once in socket programming srinietrx Programming 5 08-20-2017 12:53 PM
Socket programming - UDP broadcast client - how to receive responses - Python 0x53h Programming 2 12-16-2013 03:16 PM
[SOLVED] Can send and receive plain text mail, but can't receive on html pravada Linux - Software 1 05-17-2013 04:13 PM
Postfix : mail cannot send to send outside ( can send/receive locally) bobbinsupport Linux - Networking 3 12-15-2007 11:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration