Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C Programming

Name: Nathan 2007-04-24 21:00 ID:bb5qRGK9

Hi I am an student. I have this assignment due soon and I need to get it done in Visual C. Pretty much my part of it is to create the threads side of it and the sockets. What is the simplest way to create a thread and sockets ?

thankyou

Name: Anonymous 2007-04-24 21:08 ID:WhQTMVQG

Tcl has a nice socket library, but everything else about that language sucks. Copy the socket code.

Name: Anonymous 2007-04-24 21:16 ID:N8cC75Zq

Better start working.  I think you are screwed.

Name: Anonymous 2007-04-24 21:26 ID:bb5qRGK9

So there isn't any like way using the libraries already with C to achieve this ?

to anon: yes i am screwed.

Name: Anonymous 2007-04-25 0:09 ID:ru/CjhPS

Yes, you use the socket library.

$ man socket

Name: Anonymous 2007-04-25 0:44 ID:aL0v53R1

Multithreaded server? Listen on a port until you get a connection, which will net you a socket to the client. Manage that socket in a new thread in the server app.

Name: noob 2007-04-25 2:18 ID:JZ0AMetO

Where do i get a c compiler?

Name: Anonymous 2007-04-25 4:40 ID:BNXZArPH

>>7
I think I have one in my trash.. you can get it if you want

Name: Anonymous 2007-04-25 7:29 ID:t8yvfUuK

>>7
GCC
If you need VC, eMule

Name: Anonymous 2007-04-25 9:21 ID:kafXq6he

>>9

Or Visual C++ Express, for free

Name: Anonymous 2007-04-25 9:53 ID:2MumJKaP

lol yes you are screwed.

Name: Anonymous 2007-04-25 9:57 ID:+j7oettQ

>>1
Why you guys tell him he is screwed?
This task is fucking easy
Here's an example from beej


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

#define PORT 9034   // port we're listening on

int main(void)
{
    fd_set master;   // master file descriptor list
    fd_set read_fds; // temp file descriptor list for select()
    struct sockaddr_in myaddr;     // server address
    struct sockaddr_in remoteaddr; // client address
    int fdmax;        // maximum file descriptor number
    int listener;     // listening socket descriptor
    int newfd;        // newly accept()ed socket descriptor
    char buf[256];    // buffer for client data
    int nbytes;
    int yes=1;        // for setsockopt() SO_REUSEADDR, below
    socklen_t addrlen;
    int i, j;

    FD_ZERO(&master);    // clear the master and temp sets
    FD_ZERO(&read_fds);

    // get the listener
    if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    // lose the pesky "address already in use" error message
    if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes,
                                                        sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }

    // bind
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = INADDR_ANY;
    myaddr.sin_port = htons(PORT);
    memset(&(myaddr.sin_zero), '\0', 8);
    if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    // listen
    if (listen(listener, 10) == -1) {
        perror("listen");
        exit(1);
    }

    // add the listener to the master set
    FD_SET(listener, &master);

    // keep track of the biggest file descriptor
    fdmax = listener; // so far, it's this one

    // main loop
    for(;;) {
        read_fds = master; // copy it
        if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
            perror("select");
            exit(1);
        }

        // 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 == listener) {
                    // handle new connections
                    addrlen = sizeof(remoteaddr);
                    if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr,
                                                             &addrlen)) == -1) {
                        perror("accept");
                    } else {
                        FD_SET(newfd, &master); // add to master set
                        if (newfd > fdmax) {    // keep track of the maximum
                            fdmax = newfd;
                        }
                        printf("selectserver: new connection from %s on "
                            "socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
                    }
                } else {
                    // handle data from a client
                    if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
                        // got error or connection closed by client
                        if (nbytes == 0) {
                            // connection closed
                            printf("selectserver: socket %d hung up\n", i);
                        } else {
                            perror("recv");
                        }
                        close(i); // bye!
                        FD_CLR(i, &master); // remove from master set
                    } else {
                        // we got some data from a client
                        for(j = 0; j <= fdmax; j++) {
                            // send to everyone!
                            if (FD_ISSET(j, &master)) {
                                // except the listener and ourselves
                                if (j != listener && j != i) {
                                    if (send(j, buf, nbytes, 0) == -1) {
                                        perror("send");
                                    }
                                }
                            }
                        }
                    }
                } // it's SO UGLY!
            }
        }
    }
   
    return 0;
}

Name: Anonymous 2007-04-25 12:16 ID:GIgKyTqU

>>12
Why you guys tell him he is screwed?
Here's a hint: this board is full of idiots who can't program for shit.

Name: Anonymous 2007-04-25 12:27 ID:ru/CjhPS

>>13

I have 10+ years of experience and have been involved in multiple high-profile projects. I'm just an asshole to students who post their assignments on message boards instead of actually learning it themselves.

CS *should* have a high learning curve. Dumbass programmers who shouldn't have gotten a degree in the first place, but who skated through college by getting all their answers from the internet, waste competent programmers' time and money because we have to fix their broken code after they've made a mess of everything.

Name: Anonymous 2007-04-25 12:30 ID:kafXq6he

>>12

It's nice to see someone helping but he did ask for threads, rather than using select().

>>14

And you're just a prick. He didn't ask us to do his homework for him, he asked for advice on how to approach it. Which >>6 did.

Name: Anonymous 2007-04-25 13:41 ID:2MumJKaP

>>12

Not a solution, although i suspect it would work, it's not using threads and therefore fails the assignment.

Name: Anonymous 2007-04-25 14:57 ID:ZIaF+r7+

Write it in Python, use py2exe, disassemble, port ASM to C

Name: Anonymous 2007-04-25 17:55 ID:GIgKyTqU

>>14
Then why are you on this forum in the first place?  This isn't the typical programming message board, and there's plenty of shit that's more annoying than a student hoping for help with an assignment.  There is no reason for you to be an asshole instead of ignoring the thread, unless you aren't really a "competent programmer", and you are threatened by other people learning how to program.  Faggot.

Name: Anonymous 2007-04-25 19:59 ID:Heaven

>>18

Trolled.

Name: Nathan 2007-04-26 2:05 ID:1Gzlfbf9

hey guys thanks for the replies, although I only checked this now, I figured out how to do the sockets part at least, working on the threads now,

cheers for your directional advice in accomplishing these tasks of great difficulty !

Name: Anonymous 2007-04-26 5:54 ID:tRV50LVA

>>18
Welcome to...

4chan

Name: Anonymous 2009-03-06 11:47

1 2 3 4 5 6 7   8 9 J   K R T   BBCODE P R   I S E   x int main   int argc char   argv argc return.

Name: Sgt.Kabu笊㢀kiman塆﨏 2012-05-29 1:01

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List