LinuxQuestions.org
Help answer threads with 0 replies.
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 01-22-2020, 12:10 PM   #1
end
Member
 
Registered: Aug 2016
Posts: 266

Rep: Reputation: Disabled
Socket client with select


hi

i try put select this way but when i recive message it exit loop and shutdown.

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> 
#include <fcntl.h>
#include <errno.h>

int main(int argc , char *argv[]  )
{
	int sockett;
	struct sockaddr_in server;
	char buffer[1024];	
	char bufferr[1024]="hi server";	
	int result;
	fd_set readset;	
	
	if(argc<3)
	{	
		printf("Usage ip port:: a.out 127.0.0.1 80\n");
		exit(0);
	}

	sockett=socket(AF_INET,SOCK_STREAM   ,0);

	if (sockett<0)
                printf("SOCKET ERROR\n");
	else
		printf("SOCKET_CREATED\n");
	

	server.sin_family=AF_INET;
	server.sin_port= htons(atoi(argv[2]));
	server.sin_addr.s_addr =inet_addr(argv[1]);	



	int ret;
	ret=connect(sockett, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
	if( ret < 0 )
	{	printf("NOT_CONNECTED\n");
	}
	else
		printf("CONNECTED\n");
	
	FD_ZERO(&readset);
        FD_SET(sockett,&readset);
        

	
	while(ret);
	{

	result=select(sockett + 1, &readset ,0 ,0 ,0);

        if(result <0)
        {

                printf("SELECT_ERROR\n");
        }
	else
		printf("SELECT\n");
		FD_ISSET(sockett,&readset);
		printf("fdisset\n");
		
		result=recv(sockett, buffer, 1024, 0);
		printf("%s\n",buffer);
	
	}


	//send(sockett,bufferr,1024,0);		
	//recv(sockett, buffer, 1024, 0);
	//printf("%s\n",buffer);
	
	
}
 
Old 01-22-2020, 12:22 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by end View Post
hi

i try put select this way but when i recive message it exit loop and shutdown.

Code:
while(ret);
If you copied your code correctly into your question, this IS your loop.

I'm serious, please look at your syntax there.

Once you get my point, I think you'll understand why the loop is exiting, because this is how it is coded.
 
3 members found this post helpful.
Old 01-22-2020, 12:23 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Note also that you will eventually have mis-matching brackets, once you fix that. Please review how you've coded your else clause.
 
1 members found this post helpful.
Old 01-22-2020, 12:32 PM   #4
end
Member
 
Registered: Aug 2016
Posts: 266

Original Poster
Rep: Reputation: Disabled
yes you are right i didnt see that. thanks

working

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> 
#include <fcntl.h>
#include <errno.h>

int main(int argc , char *argv[]  )
{
	int sockett;
	struct sockaddr_in server;
	char buffer[1024];	
	char bufferr[1024]="hi server";	
	int result;
	fd_set readset;	
	
	if(argc<3)
	{	
		printf("Usage ip port:: a.out 127.0.0.1 80\n");
		exit(0);
	}

	sockett=socket(AF_INET,SOCK_STREAM   ,0);

	if (sockett<0)
                printf("SOCKET ERROR\n");
	else
		printf("SOCKET_CREATED\n");
	

	server.sin_family=AF_INET;
	server.sin_port= htons(atoi(argv[2]));
	server.sin_addr.s_addr =inet_addr(argv[1]);	



	int ret;
	ret=connect(sockett, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
	if( ret < 0 )
	{	printf("NOT_CONNECTED\n");
	}
	else
		printf("CONNECTED\n");
	
	FD_ZERO(&readset);
        FD_SET(sockett,&readset);
        

	
	while(1)
	{

	result=select(sockett + 1, &readset ,0 ,0 ,0);

        if(result <0)
        {

                printf("SELECT_ERROR\n");
        }
	else
		printf("SELECT\n");
		FD_ISSET(sockett,&readset);
		printf("fdisset\n");
		
		result=recv(sockett, buffer, 1024, 0);
		printf("%s\n",buffer);
	
	}


	//send(sockett,bufferr,1024,0);		
	//recv(sockett, buffer, 1024, 0);
	//printf("%s\n",buffer);
	
	
}
 
  


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
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 07:56 PM
Connecting client socket to server socket only once in socket programming srinietrx Programming 5 08-20-2017 11:53 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Select() did not select my socket thvo Programming 1 05-08-2005 12:20 AM
Socket and Select() problems strikernzl Programming 2 09-01-2003 08:34 AM

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

All times are GMT -5. The time now is 05:27 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