Sponsored Content
Full Discussion: USB-Ethernet/Ethernet-USB
Top Forums UNIX for Advanced & Expert Users USB-Ethernet/Ethernet-USB Post 302903759 by fedora18 on Thursday 29th of May 2014 05:56:01 PM
Old 05-29-2014
intresting stuff. ya this device I have broacasting over ethernet needs to be asked as well. you send it a string and it sends some binary data back

---------- Post updated at 11:44 AM ---------- Previous update was at 11:31 AM ----------

Okay, been doing some toying here. This is probably going to be very obvious but something isn't working. I have been trying to setup a server that uses the select call to handle multiple client connections.

I use a master socket which listens on a port/IP and adds incoming connections to an array of child connections.

My client application simply reads from the socket over and over. What I want to do is once a new client connection is made on the server simply continue to send a message to that client. Then the client just printf's the message to the console.

Once I get a bare-bones version going I have a couple of different applications to apply this to.

Thanks a lot in advance. If I have done anything dumb...go easy Smilie

---------- Post updated at 11:46 AM ---------- Previous update was at 11:44 AM ----------

---------- Post updated at 11:48 AM ---------- Previous update was at 11:46 AM ----------


SERVER:

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

int g_portno = 40000;

int main (int argc, char *argv[]){


	FILE * error;
	error = fopen("server_error.txt","w");
    int i;

    //Parse command line:
    /*if(argc != 2){
        printf("Please specify how clients will connect?\n");
        exit(0);
    }
    int num_connections,i;
    num_connections = atoi(argv[1]);
    printf("num connections: %i\n",num_connections);
    */

	//CREATE SOCKET:
	int master_socket = socket(AF_INET,SOCK_STREAM,0);
	if(master_socket<0){
		fprintf(error,"socket create fail\n");
		exit(0);
	}

    //initialize all client_socket[] to 0 so not checked:
    int max_clients = 30;
    int client_socket[30];
    for(i=0;i<max_clients;i++){
        client_socket[i] = 0;
    }

    int opt=1;
    if(setsockopt(master_socket,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt))<0){
        fprintf(error,"setsockopt fail\n");
        exit(0);
    }

	struct sockaddr_in serv_addr;
	memset(&serv_addr, '0', sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(g_portno);

    //BIND:
    int bd = bind(master_socket,(struct sockaddr*)&serv_addr, sizeof(serv_addr)); 
    if(bd<0){
    	fprintf(error,"bind error\n");
    	exit(0);
    }

    //LISTEN:
    int l = listen(master_socket,10);
    if(l<0){
    	fprintf(error,"listen error");
    	exit(0);
    }

    char buffer[1024];
    bzero(buffer,1024);
    
    int max_sd,sd,activity,s;
    fd_set readfds;


    while(1){


        //clear the socket set:
        FD_ZERO(&readfds);

        //add master_socket to set:
        FD_SET(master_socket,&readfds);
        max_sd = master_socket;

        //add child sockets to set:
        for(i=0;i<max_clients;i++){

            //socket descriptor:
            sd = client_socket[i];

            //if valid socket desrciptor then add to read list:
            if(sd > 0) FD_SET(sd,&readfds);

            //highest file descrptor number, need it for the select function:
            if(sd > max_sd) max_sd = sd;
        }

        //wait for an activity on one of the sockets, timeout is NULL, so wait indefinitely.
        activity = select(max_sd + 1,&readfds,NULL,NULL,NULL);
        if(activity < 0 ){
            fprintf(error,"select error\n");
            exit(0);
        }

        //if something happened on the master socket, then its an incoming connection:
        if(FD_ISSET(master_socket,&readfds)){

            int newsockfd = accept(master_socket, (struct sockaddr*)NULL, NULL); 
            if(newsockfd<0){
                fprintf(error,"accept fail\n");
                exit(0);
            }

            printf("new connection established on fd: %i\n",newsockfd);

            //add new socket to array of sockets:
            for(i=0;i<max_clients;i++){
                if(client_socket[i] == 0){
                    client_socket[i] = newsockfd;
                    printf("Adding to list of sockets as: %d\n",i);
                    break;
                }
            }

            snprintf(buffer,1024,"connected...\n");
            send(newsockfd,buffer,strlen(buffer),0); 

        }

        for(i=0;i<max_sd;i++){

            if(FD_ISSET(client_socket[i],&readfds)){                

                snprintf(buffer,1024,"Hello from server client %i\n",i);
                s = send(client_socket[i], buffer, strlen(buffer),0); 
                if(s==-1){
                    fprintf(error,"send fail\n");
                    exit(0);
                }

            }
        
            sleep(1);
        }

	}

	return 0;
}

---------- Post updated at 11:51 AM ---------- Previous update was at 11:48 AM ----------

CLIENT:

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

int g_portno = 40000;

int main (int argc, char *argv[]){

	FILE * error;
	error = fopen("client_1_error.txt","w");

	if(argc != 2){
             printf("Need an IP address\nTry again please\n");
             exit(0);
        } 

	//CREATE SOCKET:
	int client_1_socket = socket(AF_INET,SOCK_STREAM,0);
	if(client_1_socket<0){
		fprintf(error,"socket create fail\n");
		exit(0);
	}

	struct sockaddr_in serv_addr; 
	memset(&serv_addr, '0', sizeof(serv_addr));

	serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(g_portno);
        if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0){
             fprintf(error,"inet_pton error occured\n");
             exit(0);
       } 

    int connection = connect(client_1_socket,(struct sockaddr *)&serv_addr, sizeof(serv_addr));
    if(connection<0){
    	fprintf(error,"connection problem\n");
    	exit(0);
    }

    char buffer[1024];
    bzero(buffer,1024);

    while(1){    	

    	int n = read(client_1_socket,buffer,sizeof(buffer));

    	if(client_1_socket<0){
    		fprintf(error,"socket has closed\n");
    		exit(0);
    	}

        if(n<0){
            printf("read problem\n");
            exit(0);
        }

        printf("%s",buffer);

    }





	return 0;

}

---------- Post updated at 05:56 PM ---------- Previous update was at 11:51 AM ----------

Ok, all good.
Just needed to create another fd_set to monitor writeable sockets.

Last edited by fedora18; 05-29-2014 at 12:52 PM.. Reason: Indentation
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ethernet

We are trying to Establish Network using Linux.Now we are facing the problem in configring Ethernet.In out network we are giving eth0 in server and activating it at the boot time,so it is activating as it is but when we define the Ethernet of node on server and allowing it to activate at boot time... (5 Replies)
Discussion started by: at_renai2001
5 Replies

2. Solaris

Quad Ethernet

I have a Quad Ethernet card in a 220R. 2 ports activated. Each has its own hostname file and both hostnames are in the hosts file. I want both ports to have the same IP address so we can use the 2nd port in case we lose communication on the port. On bootup, the box gives me: SIOCSLIFFLAGS:... (5 Replies)
Discussion started by: hshapiro
5 Replies

3. AIX

ethernet down

Hello everyone I have a problem with one server, has Aix 5.3 in the errpt has this message IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION F3931284 0105133009 I H ent0 ETHERNET NETWORK RECOVERY MODE EC0BCCD4 0105133009 T H ent0 ETHERNET DOWN F3931284 ... (4 Replies)
Discussion started by: lo-lp-kl
4 Replies

4. Solaris

Ethernet Port Name

I have a new Sun 440 and I am trying to configure it. Non of the Ethernet ports are enabled; when I issue "ifconfig -a" it returns nothing. Is there a way to know the available port name (e.g. ce, bge, etc.) by running a command or so ? (4 Replies)
Discussion started by: StarSol
4 Replies

5. Solaris

ethernet

my solris box network is unreachable how to solve this ipaddress assiagning and entry in vi /etc/host how to solve network is unreachable (2 Replies)
Discussion started by: tirupathi
2 Replies

6. Solaris

solaris ethernet card and mounting usb drives

I had installed soalris 10 on my dell vostro 1400.It had installed succefully. If i type ifconfig -a it is showing only my loop back adpater. So how to tell me how to mount my usb drive and how to configure my lan ethernet card,My lan ethernet card is Broadcom. Tell me step... (1 Reply)
Discussion started by: testerindia25
1 Replies

7. UNIX for Dummies Questions & Answers

USB-USB cable between linux and windows computers

Is there an easy way to setup a cross-over cable (USB-USB) between a linux box and a windows PC? My 2 machines are next to each other but I really do not want to keep transfering my files using my USB drive. Thanks! (4 Replies)
Discussion started by: Xterra
4 Replies

8. AIX

vio server ethernet to vio client ethernet(concepts confusing)

Hi In the vio server when I do # lsattr -El hdisk*, I get a PVID. The same PVID is also seen when I put the lspv command on the vio client partition. This way Im able to confirm the lun using the PVID. Similarly how does the vio client partition gets the virtual ethernet scsi client adapter... (1 Reply)
Discussion started by: newtoaixos
1 Replies
All times are GMT -4. The time now is 06:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy