This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes simple socket programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "simple socket programming" Watch "simple socket programming" New topic
Author

simple socket programming

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


The output is

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


How can I solve it?
Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
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

Joined: Sep 06, 2007
Posts: 635
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

Joined: May 25, 2009
Posts: 29
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

Joined: Sep 06, 2007
Posts: 635
I want to test this program, Is it possible my computer be server and client?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I have changed the code, But it does not stop

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Moved to Sockets and Internet Protocols.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thanks,

What do you suggest about that code doesn't stop!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


Is it endless?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Sep 06, 2007
Posts: 635
Don't know how to do that
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Sep 06, 2007
Posts: 635
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Sep 06, 2007
Posts: 635


I changed the code and does not stop.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Try calling shutdownOutput() from the client socket.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

What code have you got so far, both client and server?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Sep 06, 2007
Posts: 635


I use this code for server







I use this for client.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Did you try executing the code? What is the output/error?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I run both by netbeans:


This the result of running of sockettest:

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

Jatin Dhingra
Greenhorn

Joined: Jun 28, 2010
Posts: 29
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

Joined: Sep 06, 2007
Posts: 635
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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Sep 06, 2007
Posts: 635
I find the problem



Now It works.

Thanks everyone for help!
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I run sockettest on computer of my firend and this exception throws:



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

Joined: Oct 29, 2001
Posts: 8914
    
    8

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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Sep 06, 2007
Posts: 635
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.
 
GeeCON Prague 2014
 
subject: simple socket programming