Problem with Socket program..


 
Thread Tools Search this Thread
Top Forums Programming Problem with Socket program..
# 1  
Old 04-10-2009
Problem with Socket program..

I wrote a program which will send a message to multiple clients(i.e, broadcasting) that are connected to a server.Once when the client receives a message from the server ,the client should read a file in the server and display it in the client.The client which responds (i.e, client wants all the data stored in a file from the server )first will be served by the server(i.e,)the server should read the file stored in the server itself and send the information in the file to the client and should stop receiving the further request from other clients.
I wrote the coding for that..But its not working properly.Can somebody plz clear the bug and make my program to work in the way i wanted??

server:
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>

int main()
{
        char buf[20]="Hai";
        char buffer[20];
        int conarr[20],con=0,i,r,count,new_fd,sockfd;
        socklen_t sin_size;
        struct sockaddr_storage their_addr;
        unsigned int sock,connect,len,child_pid,sd;
        struct sockaddr_in servaddr,cliaddr;
        sock=socket(AF_INET,SOCK_STREAM,0);
        sd = socket(AF_INET,SOCK_STREAM,0);
        servaddr.sin_family=AF_INET;
        servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
        servaddr.sin_port=htons(4005);
        bind(sock,(struct sockaddr*)&servaddr,sizeof(servaddr));
        listen(sock,5);
        len=sizeof(struct sockaddr_in);
        while(1)
        {
                if((connect=accept(sock,(struct sockaddr*)&cliaddr,&len))>0)
                {
                        printf("\nENTER\n");
                        con++;
                        printf("con %d",con);
                        conarr[con]=connect;
                        for(i=1;i<=con;i++)
                        {
                         write(conarr[i],buf,sizeof(buf));
                         
                        }
                   }
          }
        char op[2];
        FILE *f;
        char pass[100];
    //char i;
        new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size);
        sin_size = sizeof their_addr;
    printf("\n hello");
    f = fopen("file.txt", "r");

    while (fgets(pass, 100, f) != NULL)
        count++;
    op[0] = count + 48;
    op[1] = '\0';
    printf("no of lines:%s", op);
    if ((send(new_fd, op, sizeof op, 0)) == -1)
        perror("re");
    fseek(f, 0, SEEK_SET);
    count = 0;
    while (fgets(pass, 100, f) != NULL) 
        {
        if ((send(new_fd, pass, 100, 0)) == -1)
        perror("l");
    }

    fclose(f);
}


Client:

Code:
 #include<string.h>
 #include<sys/socket.h>
 #include<netinet/in.h>
 #include<arpa/inet.h>
 #include<stdio.h>
 #include<netdb.h>
 #include<stdlib.h>
 int main()
 {
         struct sockaddr_in serv;
         struct addrinfo hints;
         int r,sd,nsd,f,w,c,s,snd,lis;
         char buffer[12]="hello world\n",buff[100]="YES";
         char buf[100];
         sd = socket(AF_INET,SOCK_STREAM,0);
         printf("Socket descriptor:_%d_\n",sd);
         serv.sin_family=AF_INET;
         serv.sin_addr.s_addr=inet_addr("10.142.17.123");
         serv.sin_port=htons(4005);
         s=sizeof(serv);
         c=connect(sd,(void *)&serv,s);
         if(c==0)
            printf("%d_connected_%s",c,buffer);
         else
          {
            printf("%d_%d_Error in connection\n",c,s);
            exit(1);
           }
         while(1)
         {
          r = read(sd,buffer,sizeof(buffer));
          if(r>0)
          {
          printf("Server Says :%s\n",buffer);
          printf("\n...");
          w = write(sd,buffer,sizeof(buffer));
          printf("\nSay yes...");

          break;
          }

         }
        int count = 0;
    if ((recv(sd, buf, sizeof buf, 0)) == -1)
        perror("p");
    printf("\n The no of lines in file is %s", buf);
    count = atoi(buf);
    while (count != 0) 
        {
        if ((recv(sd, buff, sizeof buff, 0)) == -1)
            perror("k");
        printf("\n%s", buff);
        count--;
    }
 }

In simple the server broadcasts a message to multiple clients.Whichever client responds first will receive the the datas in the file stored in the server.Plz help me to make this program work.

Thank u in advance
# 2  
Old 04-10-2009
Quote:
Originally Posted by vigneshinbox
I wrote a program which will send a message to multiple clients(i.e, broadcasting) that are connected to a server. Once when the client receives a message from the server ,the client should read a file in the server and display it in the client.The client which responds (i.e, client wants all the data stored in a file from the server )first will be served by the server(i.e,)the server should read the file stored in the server itself and send the information in the file to the client and should stop receiving the further request from other clients.
I wrote the coding for that..But it's not working properly. Can somebody please clear the bug and make my program to work in the way I wanted?
In what way is it not working properly?
# 3  
Old 04-11-2009
the client program should be able to retrieve data in a file in the server when the client sends a reply to the server .But its not happenning
# 4  
Old 04-14-2009
Quote:
Originally Posted by vigneshinbox
The client program should be able to retrieve data in a file in the server when the client sends a reply to the server. But it's not happening

Unless your computer actually has the IP address 10.142.17.123, then connecting to 10.142.17.123 isn't going to work. When your client and server programs are on the same machine, you can expect the address 127.0.0.1 to work.

Otherwise, please be more specific. Don't tell me what you ultimately want it to do("retrieve data in a file") but the procedure involved.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

C socket program on UNIX

Write a C socket program on UNIX where a TCP client sends/reads a time in decimal 24 hours format to the server and the server echoes the seconds, minutes, and hours in the time. Example: Client sends 18.78 hours and the server displays 18 hours, 46 minutes and 48 seconds. (1 Reply)
Discussion started by: adi_always4u143
1 Replies

2. AIX

Sample C program to Send/Recieve a file using Socket

Hi All, I urgently need a Sample C program to Send/Recieve a file using Socket. Thanks Sara (1 Reply)
Discussion started by: saraperu
1 Replies

3. UNIX for Dummies Questions & Answers

Help to run this socket program in C

i have created two files named server and client then when i run the server program it says the server is waiting(./server 5555) then when i run the client program it says "client error:connection refused" can u plz help me to run it?:( (7 Replies)
Discussion started by: kedah160
7 Replies

4. Shell Programming and Scripting

socket program

All, I am looked to develop a socket program from one Solaris server to another Solaris server to send UDP packets from a source UDP port number 2505 on the first server to the source port 2505 on the second server. Is it possible to do? What is the best way to do this? I want to set the... (1 Reply)
Discussion started by: bubba112557
1 Replies

5. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

6. UNIX for Advanced & Expert Users

problem with socket reading

I am not able to receive the message on socket in the current process when its waiting for its child to exit. code looks something like below //in one thread of the current process //thread 1 =============================================== int numBytes = read(sockid,buf,SIZE); //Now the... (2 Replies)
Discussion started by: swap007
2 Replies

7. UNIX for Advanced & Expert Users

Fedora Linux for Socket Program Development

Hi, I am trying to port a networking application to linux, I get error while binding a socket to a port, The port is not used by any application and was verified by using netstat and other tools. I tried a simple socket and bind on a unused port, but even that fails. Is there any document... (0 Replies)
Discussion started by: venkatesh.n
0 Replies

8. Programming

Help Socket program

Hi All, I'm writing a client-server socket program. the client will be an instance of the well-known telnet application. i want to implement a simple authentication between the server and the client. - the client should send this message (after the connection established): my password... (0 Replies)
Discussion started by: mhetfield
0 Replies

9. Programming

Socket Problem

Hi all, I have developed server/client application (using C) and tested it on the same machine .. but when I deploy them on different machines I get connection timeout. Well .. server machine and client machine exists on different network segments, so there is a linux firewall box to route... (3 Replies)
Discussion started by: Agent007
3 Replies

10. Programming

How can I program socket in unix?

Excuse me . I'm a beginner . In windows , MFC can be used , but how to do in Unix ? And does unix support c++like VC++ ? How can I get developing tools in Unix ? (7 Replies)
Discussion started by: sanjohn
7 Replies
Login or Register to Ask a Question