0

I understand that the reason that its sending so much is that I set the recv to 1024 and can change how much is let in by changing that, however I only want just one Hello World sent through. I tried searching the question and nothing really helpful came up. Thanks in advance!!

Server:

#!/usr/bin/python
import socket
import sys


class Server:
    def __init__(self, sock=None):
        if sock is None:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            print("socket created")

    def bind(self, HOST, PORT):
        try:
            self.sock.bind((HOST, PORT))
        except socket.error as msg:
            print("Bind failed. Error Code : " +
                    str(msg[0]) + "Message " + msg[1])
            sys.exit()
        print("Socket bind complete")

    def main_loop(self):
        self.sock.listen(10)
        print("Socket now listening")
        while 1:
            conn, addr = self.sock.accept()
            print("Connected with " + addr[0] + ":" + str(addr[1]))
            data = conn.recv(1024)
            print(data)
        self.sock.close()

    def main(self):
        HOST = ''
        PORT = 8888
        self.bind(HOST, PORT)
        self.main_loop()


def main():
    server = Server()
    server.main()


if __name__ == "__main__":
    main()

Client

#!/usr/bin/python
import socket
import sys


class Client:
    def __init__(self, sock=None):
        if sock is None:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            print("socket created")

    def connect(self, HOST, PORT):
        try:
            self.sock.connect((HOST, PORT))
        except socket.error as msg:
            print("Connect failed. Error Code : " +
                    str(msg[0]) + "Message " + msg[1])
            sys.exit()
        print("Socket connect complete")

    def main_loop(self):
        while 1:
            self.sock.sendall(b'Hello world')
        self.sock.close()

    def main(self):
        HOST = ''
        PORT = 8888
       self.connect(HOST, PORT)
       self.main_loop()


def main():
    client = Client()
    client.main()


if __name__ == "__main__":
    main()

Server Output:

socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:45676
b'Hello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello worldHello world'

Client Output:

socket created
Socket connect complete

1 Answer 1

2

I suspect the problem is in the client, not the server:

def main_loop(self):
    while 1:
        self.sock.sendall(b'Hello world')
    self.sock.close()

When the main_loop method is called, it will send 'Hello world' until the user intervenes or the process dies or the computer wedges or the heat death of the universe, which ever happens first.

1
  • Fixed it thanks very much!!! Just a dumb mistake. Thanks for looking at it for me!
    – Kenton
    Oct 29, 2015 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.