Howe implement while loop for send and recive message in c
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Howe implement while loop for send and recive message in c
hi
howe in while loop make comunication boath side automatic i need press enter or send message to recive message. i test it with netcat nc and canot recive message before i send. howe to write while loop.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include<arpa/inet.h>
int main(int argc , char *argv[] )
{
int sockett;
struct sockaddr_in server;
char buffer[1024];
char bufferr[1024];
int rcv;
sockett=socket(AF_INET, SOCK_STREAM,0);
server.sin_family=AF_INET;
server.sin_port= htons(80);
server.sin_addr.s_addr =inet_addr(argv[1]);
int ret =-1;
ret = connect(sockett, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
while(1 )
{
if( recv(sockett,buffer,1024,0) >0 )
printf("%s",buffer);
if (send(sockett,bufferr,strlen(bufferr),0) >0 )
scanf("%s",bufferr,1024);
}
}
You should read the manpage for each of the system calls you are making. You need to be checking the return values so you can tell if they are succeeding before proceeding.
Reading the manpage will also give you an idea of what to expect from the call. For instance, unless the sockoption is set to make recv non-blocking it will block until data arrives.
Sockets can be used to communicate between threads or processes which is not what you are doing here. After reading some more you will probably want to divide your program into a client and a server program and try again.
You'd do well to read a good tutorial on client/server socket programming. There area some concepts you'll need to go forward but I'll try to summarize some.
There are different protocols used in socket communication. The primary socket types are the datagram and stream sockets (UDP and TCP respectively) and the domains are unix/local or internet.
TCP is a connected protocol and that is what SOCK_STREAM refers to. AF_INET is the internet (inet) domain for v4 internet addresses.
A basic TCP internet server will request a socket descriptor from the operating system (man 2 socket), bind the socket descriptor to a sockaddr (man 2 bind), convert the socket to a listening socket (man 2 listen) and then signal the operating system readiness to accept connections (man 2 accept). After accepting a connection the server will read and write according to whatever protocol is being used.
There, of course, are many variations to this.
Here's an example I keep hanging around on my hard drive. It accepts either v4 or v6 addresses and can be terminated by sending it a sighup. If you compile and run it form a command line, with a port as an argument, then point a browser at it you get hello world for your troubles.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.