0

I am trying to send a string encoded in utf to a server and get a response from it.However I am not able to get any response as i think the string I'm sending from the client has not been encoded properly. Here is what I've done to encode the string:

a= "!@#$%"
u = a.encode('utf-8')
s=socket.socket()
s.connect((ipAddr,portNum))
a=s.recv(1024)
print (a)//prints ok

s.send(u)
s.recv(1024)#blank
print (s.recv(1024))

JAVA:

Socket smtpSocket = new Socket(ipAddr,portNum);
           smtpSocket.setSoTimeout(1000*30);
           is = new BufferedReader(new InputStreamReader(smtpSocket.getInputStream()));
           service=new DataOutputStream(smtpSocket.getOutputStream());
           String response = is.readLine();
           System.out.println(response);


           if(response.startsWith("okE"))
           {
                  service.writeUTF(x);
           }     


response = is.readLine();
           System.out.println(response);
Rock
  • 27
  • 1
  • 8
  • Have you actually connected the socket to the server? – DYZ Feb 19 '18 at 06:23
  • Yeah I've done that,sorry i didn't mention it.I'll make the edits – Rock Feb 19 '18 at 06:25
  • Do you have any evidence that the server received your request? – DYZ Feb 19 '18 at 06:32
  • Yes, Ive skipped that part but the i received a reply from the server when i sent another request,I've made the edits – Rock Feb 19 '18 at 06:35
  • Possible duplicate of [Python Socket Flush](https://stackoverflow.com/questions/4407835/python-socket-flush) – DYZ Feb 19 '18 at 06:37
  • I just went through that and I don't think it helps.I used sendall() too but had the same result – Rock Feb 19 '18 at 06:40

1 Answers1

0

It seems that you server is writen in Java, I have write some server code that fit for you Java client.

ServerSocket serverSocket = new ServerSocket(50001);
serverSocket.setSoTimeout(3 * 1000);
Socket socket = serverSocket.accept();
System.out.println("Receive new connection: " + socket.getInetAddress());

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
DataInputStream in = new DataInputStream(socket.getInputStream());

out.write("ok");
// out.newLine();
out.flush();

String input = in.readUTF();
System.out.println(input);

out.write(input);
// out.newLine();
out.flush();

And the reason of getting nothing response in Python client is in.readUTF(), According to https://stackoverflow.com/a/48266697/7944150, you should send the length of message before.

import socket

ipAddr = 'localhost'
portNum = 50001
a = "!@#$%"
u = a.encode('utf-8')
s = socket.socket()
s.connect((ipAddr, portNum))
a = s.recv(1024)
print(a)

s.send(len(u).to_bytes(2, byteorder='big'))
s.send(u)
a = s.recv(1024)
print(a)

And I get the response:

b'ok'
b'!@#$%'
Aki Akise
  • 304
  • 2
  • 8
  • Sorry I have that in my code,I may have missed adding that here,look at my edit.I am have send.recv() but I am not getting any response – Rock Feb 19 '18 at 07:26
  • But you have not save or print out the response in your edit. Try `print(s.recv(1024))` – Aki Akise Feb 19 '18 at 07:55
  • Still no response – Rock Feb 19 '18 at 08:54
  • Print is not an issue – Rock Feb 19 '18 at 09:01
  • In your edit `s.recv(1024);print (s.recv(1024))`, your have received the response in the first `s.recv(1024)` and droped the response. And **you are trying to print another response**. That is why there is no response being printed. Could you please try my code above? If the code I provided is not working, please show you server code and inform me. – Aki Akise Feb 19 '18 at 09:34
  • I tried your code,still no response.I don't have the server code.I'm trying to connect to a remote server – Rock Feb 19 '18 at 09:36
  • So maybe the problem is in the server code, I will edit the answer and show my test code. – Aki Akise Feb 19 '18 at 09:48
  • No the there is no server code.There is a port on the server that is always open.I have a java code that works fine.I am trying to convert it to python.I am struggling with the socket.writeutf command in java.Can't think of a way to mimic that in python. – Rock Feb 19 '18 at 09:52
  • If no server code, it is hard to mimic. There is nothing wrong with encode. Can you post the java code? – Aki Akise Feb 19 '18 at 10:01
  • I've solved this problem by using an answer in another stackoverflow question.i'll still give your code a try. – Rock Feb 20 '18 at 08:47