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> |
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. -Greg. |
hi
hi i try with nonblocking on socket with fcntl but then i get connect error from connect()
i try with this also but same connect() error. Code:
sockett=socket(AF_INET,SOCK_STREAM | SOCK_NONBLOCK ,0); Code:
#include <stdio.h> |
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. Code:
|
All times are GMT -5. The time now is 07:26 AM. |