Excuse my long post. I would post code so that it is easier to understand the problem I am facing. It seems that if a signaled socket is added to epoll instance, epoll_wait on the epoll instance will not block. The following example has let me believe this:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <sys/epoll.h>
int MAX_EVENT_COUNT = 10;
int main(int argc, char *argv[])
{
int epollfd = epoll_create( MAX_EVENT_COUNT );
if ( epollfd == -1 )
{
printf("\n Error : epoll_create \n");
return 1;
}
epoll_event ev;
memset(&ev,0,sizeof(ev));
ev.events = EPOLLIN | EPOLLET;
int pipefd[2];
if (pipe(pipefd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
char ch = 'a';
write(pipefd[1], &ch, 1);
if (epoll_ctl( epollfd , EPOLL_CTL_ADD, pipefd[0], &ev ) == -1 )
{
printf("\n Error : epoll add result \n");
return 1;
}
epoll_event rawResult [MAX_EVENT_COUNT];
int32_t res = epoll_wait( epollfd, rawResult, MAX_EVENT_COUNT, -1 );
if(res!=1)
{
printf("\n Epoll problem \n");
}
else
{
printf("\n OK \n");
}
}
However the following example is pretty much the same but this time I add a signalled socket to the epoll, this time the epoll_wait call blocks
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <sys/epoll.h>
int MAX_EVENT_COUNT = 10;
int main(int argc, char *argv[])
{
int listenfd = 0;
{
struct sockaddr_in serv_addr;
char sendBuff[1025];
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
}
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
int connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
int epollfd = epoll_create( MAX_EVENT_COUNT );
if ( epollfd == -1 )
{
printf("\n Error : epoll_create \n");
return 1;
}
epoll_event ev;
memset(&ev,0,sizeof(ev));
ev.events = EPOLLIN | EPOLLET;
if (epoll_ctl( epollfd , EPOLL_CTL_ADD, sockfd, &ev ) == -1 )
{
printf("\n Error : epoll add result \n");
return 1;
}
epoll_event rawResult [MAX_EVENT_COUNT];
int32_t res = epoll_wait( epollfd, rawResult, MAX_EVENT_COUNT, -1 );
}
epoll_wait() will block if the file descriptors it is waiting on have no events for it to report about. In your case, I don't believe you have reached epoll_wait() yet. Assuming that your program has progressed passed connect() and accept(), then you have not written any data on the connection (on the sockfd), so epoll_wait() will not detect any events on the connfd.
This is different from your first program, where you write a byte of data on the pipefd[1] before calling epoll_wait() on pipefd[0].
Related
I'm very new to socket and TCP, I'm trying to send an array of Int to the server, do some sorting and calculating, then send back the result to the client and repeat.
I tried a few different ways, I either got the result after I close the client or got into a infinite loop.
What is the proper way to keep reading from the client until the client hit EOF?
Here is my server code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char const *argv[]) {
struct sockaddr_in server, client;
int sock, csock, readSize, addressSize;
char buf[256];
bzero(&server, sizeof(server));
server.sin_family = PF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(5999);
sock = socket(PF_INET, SOCK_STREAM, 0);
bind(sock, (struct sockaddr*)&server, sizeof(server));
listen(sock, 5);
addressSize = sizeof(client);
csock = accept(sock, (struct sockaddr *)&client, &addressSize);
int values[5];
while (read(csock, values, sizeof(values))) {
// Some sorting and calculating here
for (int i = 0; i < 5; i++) {
printf("%d ", values[i]);
}
}
close(sock);
return 0;
}
And here is my client code.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char const *argv[]) {
struct sockaddr_in server;
char buf[256];
int sock;
bzero(&server, sizeof(server));
server.sin_family = PF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(5999);
sock = socket(PF_INET, SOCK_STREAM, 0);
connect(sock, (struct sockaddr *)&server, sizeof(server));
while (1) {
int values[5] = {0};
for (int i = 0; i < 5; i++)
scanf("%d", &values[i]);
write(sock, values, sizeof(values));
}
return 0;
}
Thanks for your help!
On Linux, I observed that if client is terminated with Ctrl-C, then server exits when read() returns 0 to signify EOF. If client is given a Ctrl-D, the stream's error state is set and this and all future scanf calls fail without setting values. This means values retain their zero initialization, which is sent to server in each iteration of the infinite loop.
Per #user207421, recv() which I guess how read() is implemented may return on error on windows to signify and errors. In this case, server would loop with the original code.
In either case, I added error checking for most of calsl (you should also add it for inet_addr()), and the server will terminate if read() returns either -1 or 0:
server:
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>
int main(int argc, char const *argv[]) {
struct sockaddr_in server, client;
int sock, csock;
socklen_t addressSize;
bzero(&server, sizeof(server));
server.sin_family = PF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(5999);
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1) {
printf("socket: %s\n", strerror(errno));
return 1;
}
if(bind(sock, (struct sockaddr*)&server, sizeof(server)) == -1) {
printf("bind: %s\n", strerror(errno));
return 1;
}
if(listen(sock, 5) == -1) {
printf("listen: %s\n", strerror(errno));
return 1;
}
addressSize = sizeof(client);
csock = accept(sock, (struct sockaddr *)&client, &addressSize);
if(csock == -1) {
printf("listen: %s\n", strerror(errno));
return 1;
}
int values[5];
ssize_t n;
while ((n = read(csock, values, sizeof(values)))) {
printf("read %zd\n", n);
if(n <= 0) break;
for (int i = 0; i < n / sizeof(*values); i++) {
printf("%d ", values[i]);
}
printf("\n");
}
close(sock);
return 0;
}
and client:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
int main(int argc, char const *argv[]) {
struct sockaddr_in server;
char buf[256];
int sock;
bzero(&server, sizeof(server));
server.sin_family = PF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(5999);
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1) {
printf("socket: %s\n", strerror(errno));
return 1;
}
if(connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
printf("connect: %s\n", strerror(errno));
return 1;
}
while (1) {
int values[5] = {0};
for (int i = 0; i < 5; i++) {
int r = scanf("%d", &values[i]);
if(r == EOF) {
return 0;
}
}
ssize_t n = write(sock, values, sizeof(values));
if(n == -1) {
printf("write: %s\n", strerror(errno));
return 1;
}
printf("wrote %zd\n", n);
}
return 0;
}
and here is the output from the server:
$ ./server
read 20 bytes
1 2 3 4 5
and the client (note; client doesn't send partial data):
$ ./client
1
2
3
4
5
wrote 20
1
I am trying to check if a specific UDP port is open or not. I am trying to do this by sending UDP packets and checking the ICMP response to see if the UDP port is avaiable or not. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
#include <arpa/inet.h> /* inet(3) functions */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#define MAXLINE 10096
int main(int argc, char **argv)
{
int sockfd, portno;
struct sockaddr_in servaddr;
struct hostent *server;
int sendfd, recvfd;
server = gethostbyname(argv[1]);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(EXIT_FAILURE);
}
//socket varibles
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&servaddr.sin_addr.s_addr, server->h_length);
//get port from command line arguments
portno = atoi(argv[2]);
servaddr.sin_port = htons(portno);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
// open send UDP socket
if((sendfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
perror("*** socket(,,IPPROTO_UDP) failed ***n");
exit(-1);
}
// open receive ICMP socket
if((recvfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
{
perror("*** socket(,,IPPROTO_ICMP) failed ***n");
exit(-1);
}
int n;
char sendline[] = "a message"; //string for message to be sent
char recvline[MAXLINE]; //string for message to be received
//send ping request
if(sendto(sendfd, sendline, sizeof(sendline), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
{
perror("*** sendto() failed ***");
}
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 100000;
if (setsockopt(recvfd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
perror("Error");
}
n = recvfrom(recvfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = '\0'; /* null terminate */
struct iphdr *ip_hdr = (struct iphdr *)recvline;
int iplen = ip_hdr->ihl << 2;
struct icmphdr *icmp = (struct icmphdr *)((char *)ip_hdr + (4 * ip_hdr->ihl));
if((icmp->type == ICMP_UNREACH) && (icmp->code == ICMP_UNREACH_PORT))
{
printf("\nPORT CLOSED\n");
}
else
{
printf("\nPORT OPEN\n");
}
exit(0);
}
How can I get this working? When I run the code, It always says "PORT OPEN" in every port I test it with which definitely cannot be right.
I am doing a simple TCP socket programming as a test. I ported over the code from the following article:
C Socket Programming for Linux with a Server and Client Example Code
However, there are a little change I need to make, which is to set the initial sequence number (ISN) to a user-defined value on both client and server side.
Client:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
}
}
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}
Server:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}
Can someone enlighten me on which part of the code I should modify?
server.c -> always recv client data(use poll() to confirm whether an error occurred)
client.c -> always send data to server
I exec server.c and client.c ,then i try to kill client.c process.
But the POLLERR flag never be set.
The man page only say
POLLERR Error condition (output only)
When does Poll() return POLLERR?
What did I miss?
Thanks.
server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
int main()
{
int rc = 0;
char str[100];
char test[5];
int listen_fd, comm_fd;
struct sockaddr_in servaddr;
struct pollfd fds[200];
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
servaddr.sin_port = htons(22000);
bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listen_fd, 10);
comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL);
fds[0].fd = comm_fd;
fds[0].events = POLLIN | POLLOUT | POLLERR | POLLHUP;
printf("start\n");
while(1) {
rc = poll(fds,1,1000);
if (rc < 0)
printf("failed\n");
else if(rc==0)
printf("timeout\n");
else {
if (fds[0].revents & POLLERR){
printf("Error!!\n");
}
if(fds[0].revents & POLLHUP){
printf("handup!!\n");
break;
}
if (fds[0].revents & POLLIN){
int bl = recv(comm_fd,test,4,0);
printf("recv:%s %d\n",test,bl);
}
}
sleep(1);
}
return 0;
}
client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char **argv)
{
int sockfd,n;
char sendline[100];
char recvline[100];
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof servaddr);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(22000);
inet_pton(AF_INET,"127.0.0.1",&(servaddr.sin_addr));
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
while(1){
send(sockfd,"test",4,0);
sleep(5);
}
close(sockfd);
return 0;
}
It's implementation dependent. Most applications just treat POLLERR the same as normal readiness, allowing the subsequent operation to fail.
I'm trying to run server client on my 2 computers in my local network at home.
The first computer is server and the second is client.
I have error 10061 when I'm trying to connect the server. ("error - connect failed. sockfd is 164, errno is 34, WSA is 10061").
error 10061 means -"connection refused. No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host — that is, one with no server application running."
I thought it might be firewall problem so I approved in my firewall the port Im using, but stil it doesnt work.
Also, both computer have the same IP(why is that?).
Here's my code:
server.c:
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <tchar.h>
int socketBind(int sockfd, int port){
struct sockaddr_in serv_addr;
ZeroMemory((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if ( bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 ){
// we can check errno for exact ERROR
printf("bind failed with errno %d\n",errno);fflush(NULL);
return ERROR;
}
if ( listen(sockfd, 100) == -1 ){
return ERROR;
}
return 1;
}
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
printf ("Error initialising WSA.\n");
return -1;
}
int sockfd; // server's listening socket's descriptor id
int port = 4997;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
while ( sockfd < 0 ){ // ERROR
printf("Listener socket creation failed with:%d, errno is %d\n",sockfd,errno);fflush(NULL);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
if ( socketBind(sockfd, port) == ERROR ){
printf("Socket bind failed with errno=%d\n",errno);fflush(NULL);
close(sockfd);
return ERROR;
}
printf("Starting to listen to other USERS!\n");fflush(NULL);
struct sockaddr_in cli_addr;
int clilen = sizeof(cli_addr); // length of address
// accept() returns the socket that will be used for Control Connection with the accepted client
printf("*************Waiting for other USERS************\n");fflush(NULL);
int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
int readLength;
char command[(128+1)];
while(1)
{
ZeroMemory(command, sizeof(command));
readLength = read(newsockfd, command, 128+1);
if(readLength <= 0)
{
continue;
}
if(readLength > 0)
{
printf(" here should be API's func to command %s", command);fflush(NULL);
}
else
{
close(sockfd);
close(newsockfd);
WSACleanup();
printf("Read failed with errno:%d\n",errno);fflush(NULL);
return ERROR;
}
}
close(sockfd);
close(newsockfd);
WSACleanup();
return 1;
}
client.c:
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <tchar.h>
int verifyWrite(int sockfd, char* command){
int size = strlen(command);
int i=0, x=0;
for(i=0;i<size;){
x = write(sockfd, command, size);
if(x < 0){
return ERROR;
}
if(x >= 0) {
i += x;
}
}
return 0;
}
int sendToAll(char* message, int sockfd)
{
if ( verifyWrite(sockfd, message) < 0 )
{
printf("error while sending message\n");fflush(NULL);
}
return 0;
}
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
printf ("Error initialising WSA.\n");
return -1;
}
int port,sockfd;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0); //creating control connection
while(sockfd < 0){
printf("error - sockfd = %d\n",sockfd);fflush(NULL);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
port = 4997;
ZeroMemory((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.20");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
while(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0){
printf("error - connect failed. sockfd is %d, errno is %d, WSA is %d\n",sockfd,errno,WSAGetLastError());fflush(NULL);
}
printf("\n opened connection to %s\n", "192.168.1.20");fflush(NULL);
int i = 0;
while(i< 6)
{
sendToAll("just a message", sockfd);
i++;
}
WSACleanup();
return 0;
}
Your server is listening on port 4997, but your client is connecting to port 80 instead. You would have caught that if you had included the listening and connecting IP:Port pairs in your debug output to the console.