[c++] nieblokujace sockety

0

Witam

Mam nastepujacy problem mialem do napisania aplikacje seciowa na nieblokujacyxh socketach chcialemz aczac od czegosc prostego jak echo i doszedlem dop czegos takiego

#include "functions.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <errno.h>
#include <iostream>
#include <stdlib.h>
#include <fcntl.h>
using namespace std;

int main(){
	
	pid_t childpid;
	socklen_t clilen;
	int listenfd,connfd;
	struct sockaddr_in cliaddr,servaddr;
	int datasize;
	char * napis;
	listenfd = socket(PF_INET,SOCK_STREAM,0);
	string odp = "Serwer odpowiada ze : ";
	
	
	servaddr.sin_family = PF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(MYSOCK);
	
	fcntl(listenfd, F_SETFL, O_NONBLOCK);
	
	bind(listenfd,(struct sockaddr *) &servaddr,sizeof(struct sockaddr));
	
	listen(listenfd,5);
	

	while(1){
		clilen = sizeof(cliaddr);
		
		if((connfd = accept(listenfd,(struct sockaddr *) &cliaddr,&clilen))<0){
				if(errno==EINTR ){
					continue;
				}else if(errno == EINPROGRESS){
					continue;
				}
				else{
					perror("Blad");
					exit(1);
				}
		}
		
		fcntl(connfd, F_SETFL, O_NONBLOCK);
				

		if((childpid=fork())==0){
			
			
			while(1){
				if ((read(connfd,&datasize,sizeof(int)))==EWOULDBLOCK) continue;
				else break;
			}
			

			cout<<"Dlugosc nadchodzacego lancucha znakow to :"<<datasize<<endl;
			napis = new char[datasize+1];
			
			while(1){
				if ((read(connfd,napis,datasize))==EWOULDBLOCK) continue;
				else break;
			}		
			napis[datasize] = NULL;
			 
			odp = odp + napis;
			
			int retsize = odp.size();
			
			while(1){
				if ((write(connfd,&retsize,sizeof(int)))==EWOULDBLOCK) continue;
				else break;
			}
			
			while(1){
				if ((write(connfd,odp.c_str(),retsize))==EWOULDBLOCK) continue;
				else break;
			}
			
		}
		
	}
	
	
	return 0;
}
#include "functions.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <errno.h>
#include <iostream>
#include <stdlib.h>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[]){
	
	int sockfd,datasize;
	struct sockaddr_in servaddr;
	char * dane = argv[1];
	//int val;
	if(argc!=2){
		perror("Blad danych wejsciowych");
		return -1;
	}
	

	sockfd = socket(PF_INET,SOCK_STREAM,0);
	
	

    servaddr.sin_family = PF_INET;
    servaddr.sin_port = htons (MYSOCK);
    servaddr.sin_addr.s_addr = inet_addr ("127.0.0.1");
	
	fcntl(sockfd, F_SETFL, O_NONBLOCK);
	
	connect (sockfd, (struct sockaddr *) &servaddr, sizeof(struct sockaddr));
		
	

	datasize = strlen(dane);
	cout<<"Wysylam nast wartosc : "<< datasize<<endl;
	
	while(1){
		if ((write(sockfd,&datasize,sizeof(int)))==EWOULDBLOCK) continue;
		else break;
	}

	while(1){
		if ((write(sockfd,dane,datasize))==EWOULDBLOCK)
		 continue;
		else break;
	}
	
	
	int retdata;
	
	while(1){
		if ((read(sockfd,&retdata,sizeof(int)))==EWOULDBLOCK) continue;
		else break;
	}
	char * newdata = new char[retdata+1];
	while(1){
		if ((read(sockfd,newdata,retdata))==EWOULDBLOCK) continue;
		else break;
	}
	newdata[retdata] = NULL;
	
	cout<<newdata<<endl;
return 0;
}

najgorsze jest to ze po kompilacji jak probuje odpalic to to otrzymuje cos takiego

./server
Blad: Resource temporarily unavailable
make: *** [runserver] Błąd 1

Czy ktos wie co jest nie tak jak walczylem z tym wczesniej to cos tam chodzilo moze nie do konca dobrze ake cos teraz ma cos takiego, mam prosbe jezeli ktos wie co moze byc nie tak to bylbym baardzo wdzieczny

Pozdrawiam

0

a moze, banalnie, twoj port MYSOCK juz zajal sobie inny proces?

1 użytkowników online, w tym zalogowanych: 0, gości: 1