0

when using socket api to code like this:

# client end
send(string1)
send(string2)

# server end
st1 = recv(4096)
st2 = recv(4096)

The problem is that st1 will receive both string1 and string2. How can I get st2 from string2?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • You need to use different ports. Or need to parse string using some delimiter like '\n'. – Pranit Kothari Aug 01 '13 at 08:04
  • @pranitkothari contents in string1 and string2 contains '\n', the problem is that it somethimes get st2, sometimes not. – user2641102 Aug 01 '13 at 08:22
  • Yes, it is matter of chance. It is not good idea to keep two recv on on same socket. God knows which will get a chance. Get into one variable and then decide where it should go. – Pranit Kothari Aug 01 '13 at 08:25
  • @pranitkothari the logic is that clients can push and pull a lot of reqs when that want to(one may send reqs more than once), so server must seperate these reqs, how? – user2641102 Aug 01 '13 at 08:29
  • Same thing about your recv also. 'how' it understand where to go. recv just receives, then you need to take care about your data. Either use different port. Or use some delimiter which signifies where to go. If you got some other answer, please post it. I am also waiting. – Pranit Kothari Aug 01 '13 at 08:33
  • @pranitkothari hi, i use different conn in every operation, but the alg usally take lots of operations(may be overflow), what can I do?thanks! – user2641102 Aug 04 '13 at 03:02
  • If you are using multiple ports, use multi-threading, Have a look at my YouTube video, http://www.youtube.com/watch?v=hvcUVYC46mU. – Pranit Kothari Aug 04 '13 at 03:10

0 Answers0