Hi all,
I just wanna know how to send a file through a socket connection in C language
"client request a certaion file on server and server send him this file" i work on both winsockets and bsd sockets "client ==> windows, server ==> *nix"
Thanks alot :)

>I just wanna know how to send a file through a socket connection in C language
The same way you would send any other stream of characters. Just pretend the socket is an output stream and copy the file to it.

Thanks for reply,
but can u elaborate more through giving examples. can any one show me a simple code for very simple ftp client/server or any file transfer client/server in c lnaguage
Thanks again

Okay, your response begs two questions. Do you know how to read a file at all? And do you know anything about writing code for sockets?

well, my answer for this smart question u asking me if i cant code why should i be asking about sockets? "yes i can"

>u asking me if i cant code why should i be asking about sockets?
No, I was asking you what you know so we don't waste time going over things that we don't need to. But if you want to be a dick about it, just say the word and I'll ignore you so that I can do more interesting things.

>"yes i can"
This answer is worthless. So, since you seem incapable of giving me enough information to properly help you, I'll require that you post a complete program that shows an honest attempt to solve the problem.

Comments
you are the wind beneath my wings

>u asking me if i cant code why should i be asking about sockets?
No, I was asking you what you know so we don't waste time going over things that we don't need to. But if you want to be a dick about it, just say the word and I'll ignore you so that I can do more interesting things.

>"yes i can"
This answer is worthless. So, since you seem incapable of giving me enough information to properly help you, I'll require that you post a complete program that shows an honest attempt to solve the problem.

Okay I got the same prob, so maybe you can help me :)

I'm first up trying to send some simple txt files via DCC

The sending part ain't working yet and I don't get any errors :/

Can you see why?

Thanks if you do!

//File stuff

FILE *fp;
    char read[500];
    char sendDCC[500];
    if((fp = fopen(fileLoc, "r"))==NULL) {
        printf("Cannot open file.\n");    
    }

    fseek(fp, 0,SEEK_END);
    sizeFile = ftell(fp);
    ltoa(sizeFile,strSizeFile,10);

//Socket listen-receive-accept

for(;;){
        //Listen
        listenfd = listen(sockfd, 10);
        if(listenfd == 0)
            printf("-->Listening on port %s...\n\n",MYPORT);
        else
            wprintf(L"### Listen failed with error: %ld\n\n", WSAGetLastError());

        FD_ZERO(&master);    // clear the master and temp sets
        FD_ZERO(&read_fds);
        FD_SET(sockfd, &master);
        // keep track of the biggest file descriptor
        fdmax = sockfd; // so far, it's this one
for(;;) {
            read_fds = master; // copy it
            if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
                wprintf(L"### Select failed with error: %ld\n\n", WSAGetLastError());
                closesocket(4);
            }

            // run through the existing connections looking for data to read
            for(i = 0; i <= fdmax; i++) {
                if (FD_ISSET(i, &read_fds)) { // we got one!!
                    if (i == sockfd) {
                        // handle new connections
                        addr_size = sizeof their_addr;
                        new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&addr_size);
                        printf("accept");
                        if (new_fd == -1) {
                            wprintf(L"### Accept failed with error: %ld\n\n", WSAGetLastError());
                        }
                        else
                        {
                            FD_SET(new_fd, &master); // add to master set
                            if (new_fd > fdmax)
                            {    // keep track of the max
                                fdmax = new_fd;
                            }
                            printf("-->New connection from %s on socket %d\n\n", inet_ntop(their_addr.ss_family,&their_addr,remoteIP, INET6_ADDRSTRLEN),new_fd);
                        }
                    }
                    else
                    {
                        if ((nbytes = recv(i, buf, sizeof buf , 0)) <= 0) {
                            // got error or connection closed by client
                            if (nbytes == 0) {
                                // connection closed
                                printf("Socket %d hung up\n", i);
                            } else {
                                //get last error("recv");
                                printf("OK\n");
                            }
                            closesocket(i); // bye!
                            FD_CLR(i, &master); // remove from master set
                        }
                        else {
                            // we got some data from a client
                            lines = strtok(buf, "\n\r");

                            /*    do {
                            hostname = strtok(NULL, "\n\r");
                            } while (hostname[0] != 'H' || hostname[1] != 'o' || hostname[2] != 's' || hostname[3] != 't');

                            hostnamef = strtok(hostname, " ");
                            hostnamef = strtok(NULL, " ");
                            pagereq = strtok(lines, " ");
                            pagereq = strtok(NULL, " ");
                            */
                            for(j = 0; j <= fdmax; j++) {
                                // send to everyone!
                                if (FD_ISSET(j, &master)) {

                            
                                    char *mybufferke;
                                    setbuf(fp,mybufferke);
                                            //here
                                    //here
                                    //here
                                    //here
                                    //here
                                    //here
                                    //here

                                    bytes_sent = send(new_fd, mybufferke, sizeof(mybufferke), 0);
                                    if (bytes_sent > 0)
                                        printf("Send %i bytes\n\n",sizeof(bytes_sent));
                                    else if (bytes_sent == -1)
                                        wprintf(L"### Send failed with error: %ld\n\n", WSAGetLastError());

                                    // except the listener and ourselves
                                    if (j != sockfd && j != i) {

                                        if (send(j, buf, nbytes, 0) == -1) {
                                            wprintf(L"### Send failed with error: %ld\n\n", WSAGetLastError());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
This article has been dead for over six months. Start a new discussion instead.