• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Socket question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys! I'm trying to cope with th sockets but even simple actions don't work. Please give me an idea where the mistake is.



I can't also understand how socket connection works with streams: when I create BufferedReader / BufferedWriter objects in two clases how does JVM match this stream instances in different clases to each other? Is it becase of getInputStream() and getOutputStream() mathods are invoked from the same socket? Thank you in advance.
 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alex Barilo wrote:Hi, guys! I'm trying to cope with th sockets but even simple actions don't work. Please give me an idea where the mistake is.



It would probably help if you give us an idea of what is that you are seeing versus what is it that you are expecting.

Alex Barilo wrote:


I can't also understand how socket connection works with streams: when I create BufferedReader / BufferedWriter objects in two clases how does JVM match this stream instances in different clases to each other? Is it becase of getInputStream() and getOutputStream() mathods are invoked from the same socket? Thank you in advance.




Some possible issues. Your server only accepts one socket, processes one data socket, and then just exits. It doesn't even bother with closing the rendezvous socket. Your client uses localhost as the address -- which means that these two programs can only communicate when they are on the same machine. Also, it may be a good idea to flush the socket before closing them -- or better yet, don't close the socket. Instead, close the buffered writer (and buffered reader), and let the writer (reader) close the socket.

Henry
 
Alex Barilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, thank you for reply! When I flush the buffer it works, but works only if client sends the message and server receives it. But if I try to send back the message to the client it doesn't work. It looks like client and server are waiting for actions from each other and happens nothing. Frankly, I attempt to establish two-way communication between client and server - a sort of simple chat. My English not good but hope I got my points across. I would appreciate a lot if you give me an idea how to do it.
 
Henry Wong
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alex Barilo wrote:Henry, thank you for reply! When I flush the buffer it works, but works only if client sends the message and server receives it. But if I try to send back the message to the client it doesn't work. It looks like client and server are waiting for actions from each other and happens nothing. Frankly, I attempt to establish two-way communication between client and server - a sort of simple chat. My English not good but hope I got my points across. I would appreciate a lot if you give me an idea how to do it.



Well.. don't do that. Keep in mind that a protocol is more than how a packet is assembled. It is also coordination between the applications. The TCP socket will only get the packets over the wire -- but it is the responsibility of the two sides to send only when the other side expects it (and is receiving it).

Henry
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Something as following could work for Server part :



You need a server to stay active mostly of the time, so it can actually wait for the client to send something.
The code above will just bounce back the data received from the client.
 
Alex Barilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, thank you for help!
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!