Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending string via socket (python)

I have two scripts, Server.py and Client.py. I have two objectives in mind:

  1. To be able to send data again and again to server from client.
  2. To be able to send data from Server to client.

here is my Server.py :

import socket  serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "192.168.1.3" port = 8000 print (host) print (port) serversocket.bind((host, port))  serversocket.listen(5) print ('server started and listening') while 1:     (clientsocket, address) = serversocket.accept()     print ("connection found!")     data = clientsocket.recv(1024).decode()     print (data)     r='REceieve'     clientsocket.send(r.encode()) 

and here is my client :

#! /usr/bin/python3  import socket  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host ="192.168.1.3" port =8000 s.connect((host,port))  def ts(str):    s.send('e'.encode())     data = ''    data = s.recv(1024).decode()    print (data)  while 2:    r = input('enter')    ts(s)  s.close () 

The function works for the first time ('e' goes to the server and I get return message back), but how do I make it happen over and over again (something like a chat application) ? The problem starts after the first time. The messages don't go after the first time. what am I doing wrong? I am new with python, so please be a little elaborate, and if you can, please give the source code of the whole thing.

like image 372
harveyslash Avatar asked Jan 20 '14 11:01

harveyslash


People also ask

How do you send multiple messages in a socket Python?

Just run the code to start the server. The server is set to recv only 2 bytes at a time for demonstration purposes (it should be something like 8192). To send data to it import it (call it shut_srv or whatever) and call send_data for the client side.

What is sendall in Python?

sendall is a high-level Python-only method that sends the entire buffer you pass or throws an exception. It does that by calling socket. send until everything has been sent or an error occurs.


1 Answers

import socket from threading import *  serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "192.168.1.3" port = 8000 print (host) print (port) serversocket.bind((host, port))  class client(Thread):     def __init__(self, socket, address):         Thread.__init__(self)         self.sock = socket         self.addr = address         self.start()      def run(self):         while 1:             print('Client sent:', self.sock.recv(1024).decode())             self.sock.send(b'Oi you sent something to me')  serversocket.listen(5) print ('server started and listening') while 1:     clientsocket, address = serversocket.accept()     client(clientsocket, address) 

This is a very VERY simple design for how you could solve it. First of all, you need to either accept the client (server side) before going into your while 1 loop because in every loop you accept a new client, or you do as i describe, you toss the client into a separate thread which you handle on his own from now on.

like image 173
Torxed Avatar answered Sep 19 '22 04:09

Torxed