我在使用套接字时遇到问题
正如您所看到的,当我尝试将字符串从 JAVA 发送到 PYTHON 时,代码有效。
但是,当我尝试将字符串从 PYTHON 发送到 JAVA 时遇到了问题,这是相反的方式。我需要将其转换为字节并对其进行解码,因为我在发送之前对字符串进行了编码。
因此,现在的问题是当我从 Python 套接字发送字符串并通过 Java 套接字接收字符串时,我的代码如何或有什么问题?
我真的需要帮助,谢谢!
Python(服务器)代码:
import socket
import ssl
import hashlib
import os
from Crypto.Cipher import AES
import hashlib
from Crypto import Random
sHost = ''
sPort = 1234
def bindSocket():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #IPv4 and TCP
try:
s.bind((sHost,sPort))
print("Socket created and binded")
except socket.error as msgError:
print(msgError)
print("Error in Binding Socket")
return s #so that we can use it
def socketConnect():
s.listen(1) #listen to 1 connection at a time
while True:
try:
conn, address = s.accept() #Accept connection from client
print ("Connected to: " + address[0] + ":" +str(address[1]))
except socket.error as error:
print ("Error: {0}" .format(e))
print ("Unable to start socket")
return conn
def loopCommand(conn):
while True:
passphrase = "Hello Java Client "
data = conn.recv(1024)#receive the message sent by client
print(data)
conn.send(passphrase.encode('utf-8'))
print("Another String is sent to Java")
s = bindSocket()
while True:
try:
conn = socketConnect()
loopCommand(conn)
except:
pass
呼如林
相关分类