(Newbie) Help with sockets.



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!
.



Relevant Pages

  • sockets -- basic udp client
    ... import socket, sys, time ... port = socket.getservbyname ... a blank string to be sent to the client. ...
    (comp.lang.python)
  • Re: A 64-bit binary returning a value to a 32-bit binary?
    ... 64 bit via a socket and read back the string from its standard output. ... Google for IPC, ... pass the given port number to the callee. ...
    (comp.lang.c)
  • RE: [Full-Disclosure] W32/Welchia, W32/Nachi backdoor?
    ... This doesn't look like TFTP (port 65/tcp&UDP), ... for the string "system32>" It then does a directory listing on the exploited ... it pushes itself (Nachi) to the newly exploited ... then launches Nachi on the remote machine and closes the socket. ...
    (Full-Disclosure)
  • winxp multicast socket reading problem
    ... require 'socket' ... require 'ipaddr' ... sock = UDPSocket.new ... sock.bind(Socket::INADDR_ANY, PORT) ...
    (comp.lang.ruby)
  • Re: File transfer using sockets
    ... Paul Morrison wrote: ... > I am creating a program using sockets which connects to a port and sends a ... That socket then relays the message back, ... > the whole text file it sends the string END. ...
    (comp.lang.java.programmer)