Help with socket programming in C


 
Thread Tools Search this Thread
Top Forums Programming Help with socket programming in C
# 1  
Old 03-22-2010
Help with socket programming in C

hi guys i got this code trying to make connection between the server and multi clients but when i do ./server i got message server waiting then when i run ./client it says client 1 nosuch file i dont know whats that should i use any argument plz help how to compile and run and whats the expected output thanks in advance


this the client.c
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
void sig_handler(int);
int main()
{
        int sockfd;
        int len;
        struct sockaddr_un address;
        int result;
        char ch[100] ;
        char ch1[100];
        int n;
        //registering the SIGPIPE signal
        signal(13,sig_handler);
        //creating socket for client

        sockfd = socket(AF_UNIX,SOCK_STREAM,0);
        //name the socket agreed with the server
        address.sun_family = AF_UNIX;
        strcpy(address.sun_path, "server_socket");
        len = sizeof(address);
        //now connect our socket to the server's socket
        result = connect(sockfd,(struct sockaddr *)&address,len);
        if (result == -1)
        {
                perror("oops:client1");
                exit(1);
        }
        //now read and write via socket
        while(1)
        {
                printf("Enter the data\n");
                n=read(0,ch,100);
                //      printf("read return:%d\n",n);
                char close[100]= "client closed";
                if (n==0)
                {
                        write(sockfd,close,sizeof(close));
                        exit(0);
                }
                n=write(sockfd,ch,n);
                //      printf("write return:%d\n",n);
                //      sleep(3);
        //      printf("Reply from server:");
                n=read(sockfd,ch1,100);
                if (n==0)
                {
                        printf("Sorry! Server is closed\n");
                        printf("Bye!Bye!\n");
                        exit(0);
                }
                write(1,"Message from server:\n",21);
                write(1,ch1,n);
                //printf("%c",ch1);
        }
        close(sockfd);
        exit(0);
}
void sig_handler(int signo)
{
        if (signo == SIGPIPE)
        {
                printf("SIGPIPE SIGNAL RECEIVED\n");
                exit(0);
        }
}




and this is the server.c program
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#define SIZE 100
//signal handler for sigpipe signal

void sig_handler(int signo)
{
        if(signo == SIGPIPE)
        {
                printf("Signal Received\n");
        }
}
int main()
{
        int server_sockfd,client_sockfd;
        int sfd=0;
        int fd;
        int server_len,client_len;
        int select_retval;
        char buf[SIZE];
        struct sockaddr_un server_address;
        struct sockaddr_un client_address;
        struct timeval timebuf;
        fd_set read_fds,set;
        //Register the SIGPIPE signal
        signal(SIGPIPE,sig_handler);
        //Remove the old socket
        unlink("Server_Multi_TCP");
        //creating the socket
        if((server_sockfd = socket(AF_UNIX,SOCK_STREAM,0))==-1)
        {
                perror("socket error");
                exit(0);
        }
        //Naming the socket
        server_address.sun_family = AF_UNIX;
        strcpy(server_address.sun_path,"Server_Multi_TCP");
        server_len = sizeof(server_address);
        if((bind (server_sockfd,(struct sockaddr *)&server_address,server_len))==-1)
        {
                perror("bind error");
                exit(0);
        }
        //create a connection queue and waiting for clients
        if(listen(server_sockfd,5)==-1)
        {
                perror("listen error");
                exit(0);
        }
        if(server_sockfd > sfd)
                sfd = server_sockfd;
        FD_ZERO(&set);
        FD_SET(server_sockfd,&set);
        //select time value
        struct timeval tv;
        /*tv.tv_sec = 10; //number of seconds to wait
        tv.tv_usec = 500000; //number of micro seconds to wait*/
        while(1)
        {
                read_fds = set;
                printf("Server is Waiting\n");
                //Accept a connection
                if ((select_retval = select(sfd+1,&read_fds,NULL,NULL,NULL))==-1)
                {
                        perror("select error");
                        exit(0);
                }
                for (fd = 0;fd<=sfd;fd++)
                {
                        if(FD_ISSET(fd,&read_fds))
                        {
                                if(fd==server_sockfd)
                                {
                                        client_len = sizeof(client_address);
                                        if((client_sockfd = accept(server_sockfd,NULL,0))==-1)
                                        {
                                                perror("accept error");
                                                exit(0);
                                        }
                                        write(client_sockfd,"Server got your request\n",24);
                                        FD_SET(client_sockfd,&set);
                                        if(client_sockfd > sfd)
                                                sfd = client_sockfd;
                                }
                                else
                                {
                                        int nread;
                                        nread = read(fd,buf,sizeof(buf));
                                        buf[nread] = '\0';
                                        if(nread == 0)
                                        {
                                                FD_CLR(fd,&set);
                                                if(fd == sfd)
                                                        sfd--;
                                                printf("Client %d is closed\n",sfd);
                                                close(fd);
                                        }
                                        else
                                        {
                                                printf("Server got message %s from client\n",buf,sfd);
                                                write(fd,"server listening\n",17);
                                        }
                                }
                        }
                }
        }
        close(server_sockfd);
}


Last edited by jim mcnamara; 03-22-2010 at 06:00 PM.. Reason: code tags
# 2  
Old 03-23-2010
Quote:
when i run ./client it says client 1 nosuch file
Well, there's probably no such file then. Either that or the file's not an executable like you expected. Post the output of ls -l
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

socket programming

how to include socket.h in visual studio 2005.. (2 Replies)
Discussion started by: asd123
2 Replies

2. Programming

Socket programming

Hi everyone, I'm new to this forum. I'm working on new project for last few days and this forum already helped me on couple of occasions. I don't have any prior experience with network programming so I'll appreciate any advise given. I'm trying to do the following: 1. open user... (2 Replies)
Discussion started by: _thomas
2 Replies

3. UNIX for Dummies Questions & Answers

hi i need help with socket programming

in socket programming how can i : Create for example 3 blank files, namely: server, client, network •Server: act as servers/provider, will receive all requests from different client •Client: requesters •Network: middle-layer of communication between server & client any tips or... (6 Replies)
Discussion started by: kedah160
6 Replies

4. UNIX for Advanced & Expert Users

socket programming

can we send udp message to a destination ip address .. without having an ip address configured in our machine using recvfrom ? (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

5. Programming

help regarding socket programming

i m using sockets for setting up a connection between a server and a client. When the clients gets connected to the server, its ip is conveyed to the server through one of the predefined structures in c library... i save this ip address in an array....1st client's ip address goes to the zeroth... (1 Reply)
Discussion started by: abmxla007
1 Replies

6. IP Networking

socket programming

Hello Everyone Iam working on tcp/ip programming.with some time interval server has to send data.client has to close the connection and to open the connection between the time interval.this is the scenario when iam closing the connection in client side the connection terminates.how to... (1 Reply)
Discussion started by: sureshvaikuntam
1 Replies

7. IP Networking

socket programming

my system is a stand alone system... i want to try doing socket porgramming..ihave heard that this is usually done during testing... how can i do that....? (6 Replies)
Discussion started by: damn_bkb
6 Replies

8. Programming

Need Help Regarding Socket Programming

Can anyone plz me. I need a sample code for the following description. Its urgent. It is C/Socket program with the following descriptions: NAME coreadServer - Concurrent Readers Server. coreadClient - Concurrent Readers Client. SYNOPSIS coreadServer <OutputFile> coreadClient <n>... (1 Reply)
Discussion started by: priya.vmr
1 Replies

9. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

10. Programming

Socket Programming

Dear Reader, Is there any way to check up socket status other than 'netstatus ' Thanks in advance, (1 Reply)
Discussion started by: joseph_shibu
1 Replies
Login or Register to Ask a Question