LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 03-19-2005, 12:34 PM   #1
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Rep: Reputation: 30
how to set sockets as nonblock


Hi all,
I have written socket programs udpServer.c and udpClient.c . In which i write client code as

sendagain:
rc = sendto(sd,sendtext, strlen(sendtext)+1, 0,(struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr));

if(rc<0) {
printf("cannot send data \n");
close(sd);
exit(1);
}
memset(msg,0x0,1024);
cliLen = sizeof(cliAddr);
n = recvfrom(sd, recvtext, 1024, 0, (struct sockaddr *) &cliAddr, &cliLen);
if(strcmp(senttext,recvtext)!=0)
goto sendagain;

my client programs sends string to server and server sends same string back to client then client compares if both sent and received string is same or not?
if not same then i am sending same string again to server by using goto.

What i am trying to configure my udpclient work as nonblocking with timeout 5seconds.
how then can i code condition of making sockets nonblocking and only block for 5 seconds here.
 
Old 03-19-2005, 03:06 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Probably the best way is to select(2) or poll(2) on the file descriptor with a timeout of 5 seconds. If the socket has no data after those 5 seconds (you'll need to check the output of select and poll, see the man pages for exact details), act accordingly.
 
Old 03-20-2005, 12:02 AM   #3
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Original Poster
Rep: Reputation: 30
OK Finally i swapped my server and clients codes. But i am unable to use select in my udpserver program that generates string and sends to clients and clients back to server.
Please help me to make following program as UDPSERVER which is actually tcpserver program with select call used.

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define LOCAL_SERVER_PORT 9200

int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
int result;
fd_set readfds, testfds;
unsigned char msg[1024],recvtext[1024],sendtext[1024];
struct timeval timeout;
strcpy(sendtext,"mystring");


server_sockfd = socket(AF_INET, SOCK_DGRAM, 0);

server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(1500);
server_len = sizeof(server_address);

bind(server_sockfd, (struct sockaddr *)&server_address, server_len);


listen(server_sockfd, 5);

FD_ZERO(&readfds);
FD_SET(server_sockfd, &readfds);


while(1) {

int fd;
int nread;
int rc, n;
timeout.tv_sec = 5;
timeout.tv_usec = 000000;

testfds = readfds;

printf("server waiting\n");
result = select(FD_SETSIZE, &testfds, (fd_set *)0,
(fd_set *)0, &timeout);

if(result < 1) {
perror("server5");
exit(1);
}


for(fd = 0; fd < FD_SETSIZE; fd++) {
if(FD_ISSET(fd,&testfds)) {


if(fd == server_sockfd) {
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,
(struct sockaddr *)&client_address, &client_len);
FD_SET(client_sockfd, &readfds);
printf("adding client on fd %d\n", client_sockfd);
}

else {
ioctl(fd, FIONREAD, &nread);

if(nread == 0) {
close(fd);
FD_CLR(fd, &readfds);
printf("removing client on fd %d\n", fd);
}

else {
sendagain:
rc = sendto(fd,sendtext, strlen(sendtext)+1, 0,(struct sockaddr *) &server_address, sizeof(server_address));

if(rc<0) {
printf("cannot send data \n");
close(fd);
exit(1);
}
memset(msg,0x0,1024);
client_len = sizeof(client_address);
n = recvfrom(fd, recvtext, 1024, 0, (struct sockaddr *) &client_address, &client_len);
if(strcmp(senttext,recvtext)!=0)
goto sendagain;
}
}
}
}
}
}

Last edited by cranium2004; 03-20-2005 at 12:27 AM.
 
  


Reply



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
c and sockets Matir Programming 4 03-09-2005 04:15 PM
sockets pantera Programming 6 11-24-2004 06:28 AM
Sockets sibtay Programming 4 10-08-2004 09:33 AM
Set/Unset Non-Blocking sockets? zer0python Programming 2 01-04-2004 01:31 AM
sockets andox Programming 4 07-05-2003 07:04 PM

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

All times are GMT -5. The time now is 02:13 AM.

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