Teen Programmers Unite  
 

 

Return to forum top

An Instant Messenger...No it's not the same post

Posted by homegrown89 [send private reply] at July 29, 2002, 08:09:51 PM

OK, first of all I've done this so far:
Set up a username registraion page and linked it to a MySQL database running on my computer. Everything there works well.

What I'm having trouble with:
Mostly (well, alright, completely) the design of how to implement it.

What I want to do:
I want to run the "server" on my computer that will accept connections, validate the username and password sent by the "client" by checking for them in the MySQL database. Alright, simple enough.

Questions:
Once the server has validated the username and password, I want to store the socket that I used to recv() the username and password so that I can send messages to it later (i.e. when another user logs on). Also, I want to be able to keep a list of all the users logged in, which I guess is one in the same with the last sentence. Is any of this possible without threading?

My biggest problem is storing the sockets that recv() the username and password, for now.

If anyone has any resources for me, would you please send me the links, or if you have any ideas or suggestions, could you please post them.

Any help is appreciated.

Thank you,
homegrown89

Posted by Psion [send private reply] at July 29, 2002, 08:41:13 PM

First of all, I'm assuming this is in C or C++, though you didn't say. Secondly, I don't understand what you are asking.

One possible answer, depending on your question: Yes, you can store socket handles in a C program like you can anything else. If you want to store them in MySQL, then I don't really know, but that sounds like a bad idea.

Posted by homegrown89 [send private reply] at July 29, 2002, 09:08:10 PM

This is in Python.

Posted by Psion [send private reply] at July 30, 2002, 08:56:48 AM

I don't know Python, but I'd assume there is no restriction on storing sockets.

Posted by mrnorman [send private reply] at July 30, 2002, 09:27:34 PM

Too bad you aren't using Java because a Java application would handle your idea BEAUTIFULLY. The Socket.accept() method returns a Socket, which you could easily pop into an array for multi-tasking and threading (which is also amazingly easy in Java, lol). What kind of language is Python by the way, because I've heard a lot about it? Like is it interpreted, scripted, compiled? And what's it's main function(s) for use?

Posted by unknown_lamer [send private reply] at July 30, 2002, 09:31:56 PM

Python is byte compiled. It is a very dynamic, object oriented language. Zope is written in it. Lots of people use Zope. Python is good for networking I believe. I think that python has a socket.accept method too.

Posted by mrnorman [send private reply] at July 30, 2002, 09:32:28 PM

Very cool. That sounds pretty similar to Java. How is it as far as GUI's are concerned?

Posted by Psion [send private reply] at July 30, 2002, 09:42:47 PM

mrnorman, sockets are handled the same way in C and everywhere else (since they like to copy C), so it's not a particular advantage of Java.

Posted by unknown_lamer [send private reply] at July 30, 2002, 10:06:06 PM

Python has Gtk+, Qt, and tk bindings. It also has access to a few plotting and other graphics libraries. Sketch (sketch.sf.net) is written in python and gtk (for the cvs tree at least). Sketch is a vector graphics program.

Posted by mrnorman [send private reply] at July 30, 2002, 10:11:57 PM

Psion, I was just commenting on the simplified process of working with Sockets in Java (i.e. PrintWriter & BufferedReader). It's much easier to program than most languages I've seen (which is by no means extensive). Compared to programming sockets with win32 or the linux kernel, I find Java's Sockets easier to deal with. I'm sure that ther are much easier and more powerful C or C++ libraries for handling Sockets than Java has, but I have a strange, unexplainable attraction to Java, lol.

Posted by homegrown89 [send private reply] at July 30, 2002, 10:14:28 PM

I know that sockets can be stored as variables. I've written many other netword-based programs. I'll post my question to comp.lang.python.

Post-Script:
Yes, there is an accept() function in python that returns a tuple of a socket object and the address of the object.

Posted by Psion [send private reply] at July 31, 2002, 08:36:14 AM

homegrown89, it looks like your question _was_ "can I store a socket in a variable?" I'm sure we can help you if you can state it better, if that wasn't really what you are asking.

Posted by homegrown89 [send private reply] at July 31, 2002, 03:22:29 PM

As there will obviously be multiple connections, I was asking how I should store the connections when they are all made on the same socket. However, my design has now changed and hence, the problem is eliminated. Plus, the Python following here doesn't seem to be very strong.

Thanks for your time.

Posted by unknown_lamer [send private reply] at July 31, 2002, 03:38:19 PM

I don't think you quite understand sockets. A listening socket does just that--it listens for a connection. When you actually accept () the connection you are dealing with a new socket (the integer that accept returns is the file descriptor for the new socket). So what you need to do is either keep and array of the open sockets and use select + looping to deal with message or use threads to do the same thing by having one thread per connection and just doing a read() from the socket that the thread owns which will block until there is data to read or the socket's timeout is reached. The thread solution will be slightly easier but slightly more difficult (i.e. no select loop, but you have to make sure to lock shared data structures and debugging will be made almost impossible).

Posted by homegrown89 [send private reply] at July 31, 2002, 09:35:24 PM

No, I understand sockets just fine. Also, python returns more than just the file descriptor to the socket, or what would be the file descriptor, if I was writing this in C.

The information about threading and select()ing, however, is some good stuff. Unfortunately, I won't be using this design any more.

Thanks anyways.

Posted by unknown_lamer [send private reply] at July 31, 2002, 10:00:10 PM

Well, you did say that everyone would be coming over the same socket, which led me to belive you misunderstood how a listening socket works. Anyway, the accept() function in python still returns the new socket in whatever form python uses.

Posted by homegrown89 [send private reply] at August 01, 2002, 12:34:12 AM

I know.

Posted by diegoeskryptic [send private reply] at August 01, 2002, 02:22:03 PM

hey home grown... you should teach the python learning group!!!!

Posted by Psion [send private reply] at August 01, 2002, 02:39:32 PM

LG coordinators don't "teach". They set up some accessible materials for others to use on their own, and they answer occasional questions.

homegrown, but, yeah, if you think Python is lacking here, then it would be great if you would help set up TPU's Python Learning Group!

Posted by diegoeskryptic [send private reply] at August 01, 2002, 05:33:52 PM

yeah.... set up.... teach... its all the same to me.... if I dont know something... I will just ask u homegrown.... (if that is ok with you)... but I really think you should SETUP the python learning group....

Posted by Psion [send private reply] at August 01, 2002, 09:21:21 PM

Feel free to ignore the habit ds has of almost commanding people to do time consuming things.

Posted by diegoeskryptic [send private reply] at August 01, 2002, 10:22:00 PM

what the fuk?.... i didnt command him to do ne thing... i just thought it was a good idea that he take Jay-dee place in the python LG... and let it be known that u even said

"homegrown, but, yeah, if you think Python is lacking here, then it would be great if you would help set up TPU's Python Learning Group!"... Thank you very much


Posted by homegrown89 [send private reply] at August 01, 2002, 11:29:01 PM

Perhaps. How many people are actually interested?

Posted by gian [send private reply] at August 02, 2002, 01:23:07 AM

Diego: You do so!

Posted by Psion [send private reply] at August 02, 2002, 07:42:14 AM

homegrown, I think the current consensus is that Python is the best choice for a "first language," so we'd like to be able to direct all visitors who want to get started with programming to the Python LG. In that way it could become popular quickly.

All that a coordinator needs to do is pick a tutorial and set up sections on the LG page to mirror the tutorial structure. Anyone who logs in can submit exercises that coordinators can approve, though you'd probably need to create enough yourself to get the ball rolling.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.