client - server

This is a discussion on client - server within the Networking/Device Communication forums, part of the General Programming Boards category; I have decided to write client-server aplications on my own. so far I manage to do this: client.c Code: #include ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    client - server

    I have decided to write client-server aplications on my own.

    so far I manage to do this:

    client.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <winsock2.h>
    #define version 0x002
    #define MYPORT 3490
    int main()
    {
    	WSADATA wsaData;
    	SOCKET socketfd;
    	struct sockaddr_in server_addr; 
    	char message[]="Hello FANTA!";
    	if(WSAStartup(MAKEWORD(version,2),&wsaData))
    	{
    		fprintf(stderr,"\nError intializing winsock library!");
    		return 1;
    	}
    	server_addr.sin_family=AF_INET;
    	server_addr.sin_port=htons(MYPORT);
    	server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    	memset(&(server_addr.sin_zero),'\0',8);
    	
    	socketfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(socketfd==INVALID_SOCKET)
    	{
    		fprintf(stderr,"\nError creating socket!");
    		return 1;
    	}
    
    	if(connect(socketfd,(const struct sockaddr*)&server_addr,sizeof(struct sockaddr)))
    	{
    		fprintf(stdout,"Could not be connected!");
    		return 1;
    	}
    	if(send(socketfd,message,lstrlen(message),0)==SOCKET_ERROR)
    	{
    		fprintf(stderr,"Data could not be send!");
    		return 1;
    	}
    
    }
    server.c

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <winsock2.h>
    #define version 0x002
    #define MYPORT 3490
    #define bufsize 128
    int main()
    {
    	WSADATA wsaData;
    	SOCKET socketfd,new_fd;
    	struct sockaddr_in my_addr,client_sockaddr; 
    	char message[bufsize];
    	int size;
    	if(WSAStartup(MAKEWORD(version,2),&wsaData))
    	{
    		fprintf(stderr,"\nError intializing winsock library!");
    		return 1;
    	}
    	my_addr.sin_family=AF_INET;
    	my_addr.sin_port=htons(MYPORT);
    	my_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    	memset(&(my_addr.sin_zero),'\0',8);
    	
    	socketfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(socketfd==INVALID_SOCKET)
    	{
    		fprintf(stderr,"\nError creating socket!");
    		return 1;
    	}
    
    	if(bind(socketfd,(const struct sockaddr*)&my_addr,sizeof(struct sockaddr))==SOCKET_ERROR)
    	{
    		fprintf(stderr,"Error binding");
    		return 1;
    	}
    
    	if(listen(socketfd,1)==SOCKET_ERROR)
    	{
    		fprintf(stderr,"Error listening!");
    		return 1;
    	}
    	size=sizeof(struct sockaddr);
    	new_fd=accept(socketfd,(struct sockaddr*)&client_sockaddr,&size);
    	if(new_fd==INVALID_SOCKET)
    	{
    		fprintf(stderr,"\nError accepting connection!");
    		return 1;
    	}
    	while(1)
    	{
    		int get;
    		
    		get=recv(new_fd,message,bufsize-1,0);
    
    		if(get==0)
    			break;
    		else
    			if(get==SOCKET_ERROR)
    			{
    				fprintf(stderr,"\nError receiving data!");
    				return 1;
    			}
    			else
    			{
    				message[get]=0;
    				fprintf(stdout,"%s ",message);
    			}
    
    	}
    	closesocket(new_fd);
    	closesocket(socketfd);
    	WSACleanup();
    }
    As you might guess it's not working.

    I start server.exe and and after that client.exe, but nothing is happening I see no message on server window.
    I feel I'm very near.

    I'm total beginer in this stuff so any help is more than welcome.

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    The server is running on a blocking I/O model.

    What IP is the client socket connecting to?

    Kuphryn

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I doing everything on one machine. I'm trying to run both at the same time. Is there any way to make it work?

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I finally succeded.

    Server.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <winsock2.h>
    #define version 0x002
    #define MYPORT 3490
    #define bufsize 128
    int main()
    {
    	WSADATA wsaData;
    	SOCKET socketfd,new_fd;
    	struct sockaddr_in my_addr,client_sockaddr; 
    	char message[bufsize];
    	int size;
    	if(WSAStartup(MAKEWORD(version,2),&wsaData))
    	{
    		fprintf(stderr,"\nError intializing winsock library!");
    		return 1;
    	}
    	my_addr.sin_family=AF_INET;
    	my_addr.sin_port=htons(MYPORT);
    	my_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    	memset(&(my_addr.sin_zero),'\0',8);
    	
    	socketfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(socketfd==INVALID_SOCKET)
    	{
    		fprintf(stderr,"\nError creating socket!");
    		return 1;
    	}
    
    	if(bind(socketfd,(const struct sockaddr*)&my_addr,sizeof(struct sockaddr))==SOCKET_ERROR)
    	{
    		fprintf(stderr,"Error binding");
    		return 1;
    	}
    
    	if(listen(socketfd,5)==SOCKET_ERROR)
    	{
    		fprintf(stderr,"Error listening!");
    		return 1;
    	}
    	
    	while(1)
    	{
    		int get; 
    
    		printf("\nWaiting for data on port TCP: %u\n",MYPORT);
    		
    		size=sizeof(struct sockaddr);
    		new_fd=accept(socketfd,(struct sockaddr*)&client_sockaddr,&size);
    		if(new_fd==INVALID_SOCKET)
    		{
    			fprintf(stderr,"\nError accepting connection!");
    			return 1;
    		}
    		
    		
    		get=recv(new_fd,message,bufsize-1,0);
    
    		if(get==0)
    			break;
    		else
    			if(get==SOCKET_ERROR)
    			{
    				fprintf(stderr,"\nError receiving data!");
    				return 1;
    			}
    			else
    			{
    				message[get]=0;
    				fprintf(stdout,"%s ",message);
    			}
    
    	}
    	closesocket(new_fd);
    	closesocket(socketfd);
    	WSACleanup();
    }

    Client.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <winsock2.h>
    #define version 0x002
    #define SERVERPORT 3490
    int main()
    {
    	WSADATA wsaData;
    	SOCKET socketfd;
    	struct sockaddr_in server_addr; 
    	char message[]="Hello FANTA!";
    	struct hostent *h;
    	if(WSAStartup(MAKEWORD(version,2),&wsaData))
    	{
    		fprintf(stderr,"\nError intializing winsock library!");
    		return 1;
    	}
    
    	h=gethostbyname("127.0.0.1");
    	server_addr.sin_family=h->h_addrtype;
    	server_addr.sin_port=htons(SERVERPORT);
    	server_addr.sin_addr.S_un.S_addr=inet_addr(h->h_name);
    	memset(&(server_addr.sin_zero),'\0',8);
    	
    	socketfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(socketfd==INVALID_SOCKET)
    	{
    		fprintf(stderr,"\nError creating socket!");
    		return 1;
    	}
    
    	if(connect(socketfd,(const struct sockaddr*)&server_addr,sizeof(struct sockaddr)))
    	{
    		fprintf(stdout,"Could not be connected!");
    		return 1;
    	}
    	if(send(socketfd,message,lstrlen(message),0)==SOCKET_ERROR)
    	{
    		fprintf(stderr,"Data could not be send!");
    		return 1;
    	}
    
    	closesocket(socketfd);
    	WSACleanup();
    
    }
    I manage to get it work accidentaly.
    Changing code in server.c (while(1))
    if line is:
    Code:
    h=gethostbyname(INADDR_ANY);
    which I thought is a good way to obtain host infos, client simply couldn't connect.
    then I try not to use hopstent at all an to try with
    Code:
    server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    but that doesn't work either.

    It started working only after I put:
    h=gethostbyname("127.0.0.1");

    I have no experience nor enough knowledge about networks, but as far as I know IP 127.0.0.1 is IP of my computer
    when I'm offline and don't have LAN (in that case 192.168...).
    So what is the purpose of INADDR_ANY,it seems that is "0.0.0.0" but that is not problem in server.c
    In Beej's tutorial I read
    bind(), I should mention that some of the process of getting your own IP address and/or port
    can can be automated:
    my_addr.sin_port = 0; // choose an unused port at random
    my_addr.sin_addr.s_addr = INADDR_ANY; // use my IP address
    but that makes me problem in client.c

    Can you explain me this, or at least recommended some good link.
    I'm felling that I'm very close to understand this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 09:54 PM
  2. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 09:33 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  4. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21