• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

TCP connection problem

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

for my exercise i need to make a TCP connection where client sends couple of names, and server sends this names in uppercase, and cliend displays this uppercase names.
When i make a one way connection to server to display name its ok, but when i want to send this names back to client, my programm is waiting for somethin, i dont understand why,
could you suggest me something tp make it work?

when is says client connected, program is stuck...

this is my SERVER code



this is my CLIENT code

 
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of issues.

The first one is that the server has no way of knowing when the client is finished providing data.  You need to have the client and sent some kind of sentinel value to the server to indicate that it is done (empty line, single period, etc.), so that the server can stop collecting data, and then do the processing.

Another issue is that your client never actually connects to the server.  You are missing the call to Socket#connect.

Thirdly, the client expects the payload returned by the server to be a single line, but if the client provide multiple lines of data (as you have in your example code), the client will close the socket before reading all of the content provided by the server.
 
Ilya Mikh
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:There are a number of issues.



Hello again,

thank you for your help; i made it like this now and it works.

the Socket.connection - i dont understand, Socket socket = new Socket(InetAddress.getLocalHost(), 5545) <- doesn't it means that client is connecting here by InetAddress and port?

the third issue, a think a got it..

SERVER

[/code]
 
Ron McLeod
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilya Mikh wrote:the Socket.connection - i dont understand, Socket socket = new Socket(InetAddress.getLocalHost(), 5545) <- doesn't it means that client is connecting here by InetAddress and port?


Yes - you are right -- I missed that you are specifying the target endpoint when you create the socket instance.
 
Ron McLeod
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other comment .. I would have probably used PrintWriter and BufferedReader rather than Formatter and Scanner since you are not really using any of the features that those classes bring.

For example:

 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use many of the features of a Formatter with a PrintWriter since it has format() and (overloaded) printf() methods.
 
Ilya Mikh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:One other comment .. I would have probably used PrintWriter and BufferedReader rather than Formatter and Scanner since you are not really using any of the features that those classes bring.




Yes i see on many videos on youtube using these two objects; but in our school they give us, that we may better use this Scanner and Formatter.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic