This is a server code by python
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', 12000))
print(" the server is ready to receive")
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)
and the client code is
from socket import *
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = input ('Input lowercase sentense:')
clientSocket.sendto(message.encode(),(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print (modifiedMessage)
clientSocket.close()
I run the server first the output is
enter image description here
Then I run client and enter the lowercase sentence after that the client and server does't do anything else
enter image description here
please help me.
Your problem is here:
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)`
Indentation in Python is important. Your server process will never reach the third statement as it is technically positioned after the end of an infinite loop.
Related
I need to adapt the following simple code so that to achieve the following:
the client will send requests to the server to reverse strings (taken as a command
line input) over the network using sockets.
the client and the server negotiate through a fixed negotiation port (<n_port>) of the server, a random port (<r_port>) for later use. Then, later in the transaction stage, the client connects to the server through the negotiated random port (<r_port>) for actual data transfer.
Here is the code:
#TCP Server:
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('''',12020))
serverSocket.listen(1)
print 'The server is ready to receive'
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.
encode())
connectionSocket.close()
TCP Client
from socket import *
serverName = ’servername’
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024)
print modifiedMessage.decode()
clientSocket.close()
Will appreciate your help.
I have put together a server and client code to use in a messaging app. When I run the server and starts one client, everything works fine. When I start a second client, I can send messages from the first client and the second client will recieve them. I can send one message from the second client and the first client will recieve this first message. But after this message, the second client can not send or the server can not receive the data for some reason. The first client can still send messages.
I dont know where the mistake is, but I believe either the client can not .send() or the server can not .recv().
(I am quite new to programming so the code might be quite messy and not the most understandeble, and maybe there are several flaws...)
The server code
import socket
from _thread import *
import sys
HOST = "127.0.0.1"
PORT = 12000
client_socket = set()
def threaded(conn):
while True:
try:
data = conn.recv(1024).decode()
if not data:
print("Lost connection")
break
for conn in client_socket :
conn.send(data.encode())
except:
break
print("Gone")
conn.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
print("Server is up and running")
while True:
conn, addr = s.accept()
print("Connected to", addr)
client_socket .add(conn)
start_new_thread(threaded, (conn, ))
The client code
import threading
import socket, sys
HOST = "127.0.0.1"
PORT = 12000
check= ""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
def background():
while True:
answer= s.recv(1024).decode()
if check!= answer and answer!= "":
print(answer)
threading1 = threading.Thread(target=background)
threading1.daemon = True
threading1.start()
while True:
message= input()
if message!= "":
s.send(message.encode())
check = message
So far, I have been working with Sockets. One socket for sending and receiving. I've been told that that is stupid, however I was not able to find the correct way to use socketpairs on client and server from ground up. I will give my current way, and hope someone could tell me how to do socketpairs with this. Using TCP by the way.
#client
import socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = "127.0.1.1"
port = 22222
my_socket.connect((ip, port))
#server
import socket
import multiprocessing
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip_address = socket.gethostbyname(hostname)
port = 22222
s.bind((ip_address, port))
s.listen(20)
connections=[]
connection_threads=[]
while True:
clientsocket, address = s.accept()
#does this create a new socket for every incoming connection by the way?
new_client = client.Client(address, clientsocket)
connections.append(new_client)
new_process = multiprocessing.Process(target=client_butler.connect_client, args=(new_client,))
new_process.start()
connection_threads.append(new_process)
My end goal would be that the server has one socketpair for every client. I know that this is probably not good implementation as it is completely ignoring errorhandling and so on, but I will look at that in the future as all I am trying to do is me self teaching this. So the question is, how would I transform this into working with socketpairs?
Problems I have:
How do I connect the socketpair as in my client via my_socket.connect?
If I just connect both sockets individually, how do I 'reassemble' the pair in the server
Am I even doing this correctly, or is this the completely wrong approach?
Also: I checked this out, but even if I could copy the code and it would work I wouldn't know why it would work, which isn't what I want, I don't want this to work I'd also like to learn from this.
Thanks!
Referenced from Computer Networking: A Top-Down Approach, 7th Edition:
client.py:
from socket import *
serverName = "127.0.0.1"
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
sentence = input('Input lowercase sentence: ')
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024)
print('From Server: ', modifiedSentence.decode())
clientSocket.close()
server.py
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print('The server is ready to receive')
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.encode())
connectionSocket.close()
I am new to Socket programming and trying to run a simple client-server code.
server.py
host=""
port=80
from socket import *
s=socket(AF_INET, SOCK_STREAM)
s.bind((host,port))
s.listen(1)
print "Listening for connections.. "
q,addr=s.accept()
data=input("Enter data to be send: ")
q.send(data)
s.close()
client.py
host = "52.170.194.29"
port=80
from socket import *
s=socket(AF_INET, SOCK_STREAM)
s.connect((host,port))
msg=s.recv(1024)
print ("Message from server : " + msg.strip().decode('ascii'))
s.close()
# End of code
Both scripts compile successfully. As such, server.py goes blank or does nothing after printing waiting for connections and similar happens for client.py.
Can I have some idea where am I going wrong ?
My server (listening) is as:
import os
from socket import *
host = ""
port = 1337
buf = 1024
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(addr)
print("Waiting to receive messages...")
while True:
(data, addr) = UDPSock.recvfrom(buf)
data=data.decode('ascii')
print("Received message: " + data)
if data == "exit":
break
UDPSock.close()
os._exit(0)
and my client is
import os
from socket import *
host = "MY INTERNET IP"
port = 1337
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
while True:
data = input("Enter message to send or type 'exit': ")
data=data.encode('ascii')
UDPSock.sendto(data, addr)
if data == "exit":
break
UDPSock.close()
os._exit(0)
so I need to know, how do I transmit these messages over internet? i have all my ports open on my network yet i can not receive any messages while trying to do that. My server is listening on port 1337 and my client is transmitting to MY INTERNET IP where a server is listening on the port it is transmitting on. so how do I get this to work, what do i do wrong? thing is, it works while doing lan. it's cozy, but very pointless since i would like to transmit text over web
EDIT
Got my friend to run client side. Python just returned 4.
Nothing happened on my side.