(Newbie) Help with sockets.

M

mentaltruckdriver

Hi everyone. I'm fairly new to Python, and even more new to socket
programming. I think I've wrapped my head around sockets, and with
that I want to create a Telnet-based chat server, the idea being
people connect to the telnet servers with their clients and they all
communicate. I've got the code working, but the server sends each
letter to the clients on a new line! I've read about this kind of
thing on Windows on Google, but I can't find a solution to this issue.

I got the code from here - http://www.scribd.com/doc/134861/Sockets-Programming-with-python.
When I get this working I want to take my knowledge of sockets and
expand the code further.

Here's the code:

import socket
import select

class ChatServer:

def __init__( self, port ):
self.port = port;

self.srvsock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.srvsock.setsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR, 1)
self.srvsock.bind(("", port))
self.srvsock.listen( 5 )

self.descriptors = [self.srvsock]
print "ChatServer started, listening on port %s" % port

def run( self ):
while 1:
(sread, swrite, sexc) = select.select( self.descriptors,
[], [] )
for sock in sread:
if sock == self.srvsock:
self.accept_new_connection()
else:
data = ''
string = sock.recv(100)
if string == '':
host,port = sock.getpeername()
string = 'Client left %s:%s\r\n' % (host,
port)
self.broadcast_string( string, sock )
sock.close
self.descriptors.remove(sock)
else:
host, port = sock.getpeername()
newstring = '[%s:%s] %s\r\n' % (host, port,
string)
self.broadcast_string( newstring, sock )

def accept_new_connection( self ):

newsock, (remhost, remport) = self.srvsock.accept()
self.descriptors.append( newsock )

newsock.send("You're connected to the chat server!\r\n")
string = 'Client joined %s:%s\r\n' % (remhost, remport)
self.broadcast_string( string, newsock)

def broadcast_string( self, string, omit_sock ):

for sock in self.descriptors:
if sock != self.srvsock and sock != omit_sock:
sock.send(string)
print string

server = ChatServer( 2626 ).run()

Can anyone help me find a solution to this? Any help would be
appreciated.

Thanks!
 
M

Miki

Hello,
Hi everyone. I'm fairly new to Python, and even more new to socket
programming. I think I've wrapped my head around sockets, and with
that I want to create a Telnet-based chat server, the idea being
people connect to the telnet servers with their clients and they all
communicate.
IMO you should start with twisted (http://twistedmatrix.com/trac/),
which simplifies socket programming very much.

HTH,
 
G

Gabriel Genellina

Hi everyone. I'm fairly new to Python, and even more new to socket
programming. I think I've wrapped my head around sockets, and with
that I want to create a Telnet-based chat server, the idea being
people connect to the telnet servers with their clients and they all
communicate. I've got the code working, but the server sends each
letter to the clients on a new line! I've read about this kind of
thing on Windows on Google, but I can't find a solution to this issue.

The telnet client can use "line mode" or "character mode". In "line mode"
nothing is sent until the user press Enter; line editing is made on the
client side. In "character mode" keystrokes are sent as soon as typed;
probably your telnet client is using this mode.
 
G

Grant Edwards

The telnet client can use "line mode" or "character mode". In "line mode"
nothing is sent until the user press Enter; line editing is made on the
client side. In "character mode" keystrokes are sent as soon as typed;
probably your telnet client is using this mode.

That's definitely going to be a problem.


To the OP:

You claim to be writing a telnet server, yet I don't see any
code that actually implements the telnet protocol. Different
telnet clients default to different modes, so if you want them
in a certain mode, you have to put them in the desired mode by
implementing the telnet feature negotiation protocol.
 
M

mentaltruckdriver

That's definitely going to be a problem.

To the OP:

You claim to be writing a telnet server, yet I don't see any
code that actually implements the telnet protocol. Different
telnet clients default to different modes, so if you want them
in a certain mode, you have to put them in the desired mode by
implementing the telnet feature negotiation protocol.

--
Grant Edwards                   grante             Yow!  It's strange, but I'm
                                  at               only TRULY ALIVE when I'm
                               visi.com            covered in POLKA DOTS and
                                                   TACO SAUCE...

OP here - sorry for the confusion, it's just a listener program that
people connect to via telnet.

I'll take a look at the Twisted engine and see if that helps at all.
 
G

Grant Edwards

OP here - sorry for the confusion, it's just a listener program that
people connect to via telnet.

That's what a telnet server _is_: something that people connect
to via telnet.
I'll take a look at the Twisted engine and see if that helps at all.

Make sure it has telnet protocol support, or you're going to
end up with similar problem.
 
M

mentaltruckdriver

That's what a telnet server _is_: something that people connect
to via telnet.


Make sure it has telnet protocol support, or you're going to
end up with similar problem.

--
Grant Edwards                   grante             Yow! Four thousand
                                  at               different MAGNATES, MOGULS
                               visi.com            & NABOBS are romping in my
                                                   gothic solarium!!

Thanks for all the responses, the Twisted engine was exactly what I
needed.

Thanks for all your help guys!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top