LINUX.ORG.RU

Соединится с сервером по сокету?

 , , ,


0

1

Не пойму вот приложение завершается без ошибки хотя так быть не должно, прилагаю код сервера

#include <stdio.h>
#include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
	#include <netinet/in.h>
	#include <netdb.h>
	#include <arpa/inet.h>
	#include <pthread.h>
        int sockfd
        , portno
        , n
        ,*newsockfd
        ,id
        ,clients
        ,currentclient=0;
        ;
        socklen_t clilen;
        struct sockaddr_in serv_addr
        ,cli_addr;
        struct hostent *server;
        char* name;
        char buffer[2][256];
int Init()
{
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		if (sockfd < 0)
	        error("ERROR opening socket");
		bzero((char *) &serv_addr, sizeof(serv_addr));
		serv_addr.sin_family = AF_INET;
		if(id==0)
			serv_addr.sin_addr.s_addr = INADDR_ANY;
		else
			serv_addr.sin_addr.s_addr = inet_addr(name);
		serv_addr.sin_port = htons(portno);
		if(id==0)
		{
	        if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
				error("ERROR on binding");
		}
		else
		{
	        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
		        error("ERROR connecting");
		}
		if(id == 0)
		{
	        listen(sockfd,5);
		    clilen = sizeof(cli_addr);

			newsockfd[currentclient] = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
			if (newsockfd < 0)
				error("ERROR on accept");
            currentclient++;
		}
	return 0;
}
void *a(void *args)
{
    while(1)
    {
        newsockfd[currentclient] = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd[currentclient]< 0)
            error("ERROR on accept");
        currentclient++;
    }
}
void error(const char *msg)
{
    perror(msg);
    close(sockfd);
	exit(1);
}
int Close()
{
    close(sockfd);
    if(id == 0)
        close(newsockfd);
	return 0;
}
void Send(int numberclient,int numberbuf)
{
    if(id == 0)
        n = write(newsockfd[numberclient],buffer[numberbuf],255);
    else
        n = write(sockfd,buffer[numberbuf],strlen(buffer));
    if (n < 0) error("ERROR writing to socket");
        bzero(buffer,256);
}
void Recv(int numberclient,int numberbuf)
{
	if(id == 0)
		n = read(  newsockfd[numberclient] ,buffer[numberbuf],255);
	else
		n = read(  sockfd ,buffer[numberbuf],255);
    if (n < 0) error("ERROR reading from socket");
}
void SetBuf(char * s,int numberbuf)
{
    bzero(buffer[numberbuf],256);
    unsigned int i;
    for(i = 0;i<strlen(s);i++)
        buffer[numberbuf][i] = s[i];
}
void ClearBuf(int numberbuf)
{
    bzero(buffer[numberbuf],256);
}
char * GetBuf(int numberbuf)
{
    return buffer[numberbuf];
}
void SetName(char *name_)
{
    name = name_;
}
void SetPort(int portno_)
{
    portno = portno_;
}
const int length = 256;
char *text;
typedef struct
{
    int numberclient;
    int numberbuf;
}clientdata;
void s(void *numbers)
{
    clientdata *data = (clientdata*)numbers;
    data->numberbuf = 1;
    while(1)
    {
        ClearBuf(data->numberbuf);
        //Sleep(1);
        //scanf("%s",text);
        SetBuf(text,data->numberbuf);
        Send(data->numberclient,data->numberbuf);
        //if(text[0] == '0')
          //  break;
    }
}
void r(void *numbers)
{
    clientdata *data = (clientdata*)numbers;
    data->numberbuf = 0;
    while(1)
    {
        ClearBuf(data->numberbuf);
        //Sleep(1);
        Recv(data->numberclient,data->numberbuf);
        printf("%s",GetBuf(data->numberbuf));
        //if(GetBuf(data->numberbuf)[0] == '0')
           // break;
    }
}
int main()
{
    text = "azaza";
    printf("Enter clients count\n");
    scanf("%d",&clients);
    newsockfd = (int*)malloc(sizeof(int)*clients);
    pthread_t threadsend,threadreceive,threadaccept;
    text = (char*)malloc(sizeof(char)*length);
    id = 0;
	printf("input port number\n");
	int port;//=15000;
	scanf("%d",&port);
	SetPort(port);
	Init();
	pthread_create(&threadaccept,NULL,a,NULL/*(void*)currentclient*/);
	clientdata *d;
	for(int i=0;i<clients;i++)
    {
        d->numberclient = i;
        pthread_create(&threadreceive,NULL,r,(void*)d);
        pthread_create(&threadsend,NULL,s,(void*)d);
        //pthread_join(threadreceive,NULL);
        //pthread_join(threadsend,NULL);
	}
        //r(i);
	Close();
	//system("pause");
	free(text);
	free(newsockfd);
    return 0;
}

и клиента

#include <stdio.h>
#include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
	#include <netinet/in.h>
	#include <netdb.h>
	#include <arpa/inet.h>
	#include <pthread.h>
        int sockfd
        , portno
        , n
        ,*newsockfd
        ,id
        ,clients
        ,currentclient=0;
        ;
        socklen_t clilen;
        struct sockaddr_in serv_addr
        ,cli_addr;
        struct hostent *server;
        char* name;
        char buffer[2][256];
int Init()
{
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		if (sockfd < 0)
	        error("ERROR opening socket");
		bzero((char *) &serv_addr, sizeof(serv_addr));
		serv_addr.sin_family = AF_INET;
		if(id==0)
			serv_addr.sin_addr.s_addr = INADDR_ANY;
		else
			serv_addr.sin_addr.s_addr = inet_addr(name);
		serv_addr.sin_port = htons(portno);
		if(id==0)
		{
	        if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
				error("ERROR on binding");
		}
		else
		{
	        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
		        error("ERROR connecting");
		}
		if(id == 0)
		{
	        listen(sockfd,5);

		    clilen = sizeof(cli_addr);
			newsockfd[currentclient] = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
			if (newsockfd < 0)
				error("ERROR on accept");
            currentclient++;
		}
	return 0;
}
void error(const char *msg)
{
    perror(msg);
    close(sockfd);
	exit(1);
}
int Close()
{
    close(sockfd);
    if(id == 0)
        close(newsockfd);
	return 0;
}
void Send(int numberclient,int numberbuf)
{
    if(id == 0)
        n = write(newsockfd[numberclient],buffer[numberbuf],255);
    else
        n = write(sockfd,buffer[numberbuf],strlen(buffer));
    if (n < 0) error("ERROR writing to socket");
        bzero(buffer,256);
}
void Recv(int numberclient,int numberbuf)
{
	if(id == 0)
		n = read(  newsockfd[numberclient] ,buffer[numberbuf],255);
	else
		n = read(  sockfd ,buffer[numberbuf],255);
    if (n < 0) error("ERROR reading from socket");
}
void SetBuf(char * s,int numberbuf)
{
    bzero(buffer[numberbuf],256);
    unsigned int i;
    for(i = 0;i<strlen(s);i++)
        buffer[numberbuf][i] = s[i];
}
void ClearBuf(int numberbuf)
{
    bzero(buffer[numberbuf],256);
}
char * GetBuf(int numberbuf)
{
    return buffer[numberbuf];
}
void SetName(char *name_)
{
    name = name_;
}
void SetPort(int portno_)
{
    portno = portno_;
}
const int length = 256;
char *text;
typedef struct
{
    int numberclient;
    int numberbuf;
}clientdata;
void s(void *numbers)
{
    clientdata *data = (clientdata*)numbers;
    data->numberbuf = 1;
    while(1)
    {
        ClearBuf(data->numberbuf);
        //Sleep(1);
        //scanf("%s",text);
        SetBuf(text,data->numberbuf);
        Send(data->numberclient,data->numberbuf);
        //if(text[0] == '0')
          //  break;
    }
}
void r(void *numbers)
{
    clientdata *data = (clientdata*)numbers;
    data->numberbuf = 0;
    while(1)
    {
        ClearBuf(data->numberbuf);
        //Sleep(1);
        Recv(data->numberclient,data->numberbuf);
        printf("%s",GetBuf(data->numberbuf));
        //if(GetBuf(data->numberbuf)[0] == '0')
           // break;
    }
}
int main()
{
    id = 1;
	printf("input ip address\n");
	char *name = (char*)malloc(sizeof(char)*15);
	//name = "127.0.0.1";
	scanf("%s",name);
	SetName(name);
	printf("input port number\n");
	int port;//=15000;
	scanf("%d",&port);
	SetPort(port);

    text = (char*)malloc(sizeof(char)*n);
	Init();
	printf("0 - for exit!\n");
    clientdata *d;
    d->numberclient = 0;
    pthread_t threadsend,threadreceive;
    pthread_create(&threadreceive,NULL,r,(void*)d);
    pthread_create(&threadsend,NULL,s,(void*)d);
	Close();
	free(name);
	free(text);
    return 0;
}


Последнее исправление: beastie (всего исправлений: 7)

Поправь, пожалуйста, форматирование. Без него вряд ли кто что вообще что-нибудь прочитает.

turtle_bazon ★★★★★
()

Что есть id? Где он инициализируется?

mittorn ★★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.