• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple socket programming

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


The output is

run:
Couldn't get I/O for the connection to: hostname



How can I solve it?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There has to be a server app running for it to connect to, otherwise there would be no I/O connection. Since your list of details surrounding the problem are vague and sparse, need to ask whether you have a server app even running.

You could also easily test if you are even getting a connection because right now, your checks before attempting to read or write to the streams are insufficient. Checking for null won't cut it. You also need to see if it is connected with the following:

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean a program like this must be run at the server?






When I run this program, It does not stop!
 
Matthew Cox
Greenhorn
Posts: 29
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mention that the code will never stop ... almost sounded like unexpected behavior by the way you phrased it. This indeed, will never stop because you have no exit condition for the while loop, or alternatively, a method for breaking out of the loop. Fix this to allow you to terminate the app gracefully.





As for the code. I think the problem might simply be that you are setting your server app to listen on port 9999, while you are connecting with your client app on 25. Change it so that the two port numbers match.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to test this program, Is it possible my computer be server and client?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed the code, But it does not stop

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the same machine can act as both server and client. You need to have a Server Java program which you would run first. Then another Client Java program which connects to the IP address (127.0.0.1) and the port number on which the Server is running.

Update: And a Simple Socket program would be a Echo program, where you send the data to the server and the server socket would print it or send it back to the client socket.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You need to have a Server Java program which you would run first



May you explain more?

Is it possible I and a friend access to internet, and use our IP?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Sockets and Internet Protocols.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:
Is it possible I and a friend access to internet, and use our IP?



It depends on how you are accessing the internet. If you (and your friend) are directly accessing the internet, then yes, it should work. If you (or your friend) are going through some sort of address translation (behind a firewall), then you need to configure the router to allow a port through, and configure your application accordingly.

Most home router will NAT an address to 192.168.x.x, and most company firewalls will NAT an address to 10.x.x.x, so if you have any of these two IP addresses, then you have to do some more work.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

What do you suggest about that code doesn't stop!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you surprised that it doesn't stop? Why? As Matthew already explained, there is an endless while (true) loop in the program.

Do you understand what the program does, line by line?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is it endless?
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This loop has the opposite problem. It will terminate too easily. Don't use available().

Think about when you want the loop to end, and then implement that.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know how to do that
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:

You need to have a Server Java program which you would run first



May you explain more?


Its a program which sets up a ServerSocket at a certain port. And then waits for Client sockets to connect.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public int available()
throws IOException
Returns the number of bytes that can be read from this input stream without blocking.
The available method of BufferedInputStream returns the sum of the the number of bytes remaining to be read in the buffer (count - pos) and the result of calling the available method of the underlying input stream.



When there is no byte,It return zero. Why doesn't it work in that code?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you open the connection, the server doesn't immediately start sending you data. You have to send something to the server first, and then the server will send you back some data.

What your program is doing is looking if there is some data to be read, and if there isn't, then it stops. It stops immediately because right after you open the connection, the server doesn't have any data to send you. Unlike some other methods, the available() method will not make the receiver wait until there is some data: that's what "without blocking" means in the documentation.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to solve this problem for yourself, you will first have to answer this one question:

The server will be waiting for clients that want to connect to it. When do you want the server to stop listening for new clients?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I changed the code and does not stop.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try calling shutdownOutput() from the client socket.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read a tutorial in java world:

http://www.javaworld.com/jw-12-1996/jw-12-sockets.html

I copy that code to run and I change it to run on my computer.

I use netbeans,I want to test this when I am using internet,I want to be client and server at same time,

This loop was in that tutorial:



http://www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4

two days and nothing!
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What code have you got so far, both client and server?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What code have you got so far, both client and server?



Yeah, please post the code for both the client and server.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I use this code for server







I use this for client.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try executing the code? What is the output/error?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run both by netbeans:


This the result of running of sockettest:

run:
Couldn't get I/O for the connection to: hostname


 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:I run both by netbeans:


This the result of running of sockettest:

run:
Couldn't get I/O for the connection to: hostname




Is your machine connected to office LAN or anything that is blocking the particular port you are using. It could be PC firewall, LAN firewall, local LAN policies.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I turn off the firewall.

Now it shows:

Server: MAIL From: k3is@fundy.csd.unbsj.ca
Server: DATA
Server: Subject: testing
Server:




Why doesn't it show Hi there?

 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a good look at your server's loop. As a matter of fact, copy the loop and explain what every single line does for us.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Take a good look at your server's loop. As a matter of fact, copy the loop and explain what every single line does for us.



Agreed. And pay attention to what has been returned. You will notice that more than the message body is missing.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
readLine:

http://download.oracle.com/javase/1.3/docs/api/java/io/BufferedReader.html#readLine()

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

It returns first line, But it doesn't send the second...But it sends third...

Why?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find the problem



Now It works.

Thanks everyone for help!
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to run this program on internet. my computer will be client, My friend computer will be server.

Is this program runnable ? Or Do is it necessary to change something?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will need a network path from your computer to your friend's, which means that you'll have to know his public IP address (or domain name, if he has one). You'll also have to have his end configured to receive connections, which means forwarding ports in whatever device he uses to connect.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run sockettest on computer of my firend and this exception throws:

IOException: java.net.SocketException: Connection reset



Why?

I also have another question:

Every computer known by IP in network.

Socket class, for client used IP.

But server socket does not. Why?
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:
Every computer known by IP in network.

Socket class, for client used IP.

But server socket does not. Why?



ServerSocket binds to a port on the local machine, so no address is necessary.
As for your error, did you set up a network path from your machine to his? Are you trying to connect to a public IP address?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:I also have another question:

Every computer known by IP in network.

Socket class, for client used IP.

But server socket does not. Why?



I'm not sure I understand this question. But based on my guess, let me put it this way: If I want to telephone somebody, I have to know their telephone number and I have to dial their telephone number on my phone. But if I want to answer calls from other people, my phone already knows its number. I don't have to keep telling my phone what its number is.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The story is, I ran this program on his computer, for first time, His computer be as server and client,as time that I ran on my computer, On my computer it works, But on his computer it throws that exception.
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic