Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

new to sockets/networking  RSS feed

 
Stephan Norwood
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following client code:
(no specific server running at port 4242

import java.net.*;

public class client {
public void go() {
try {
Socket client = new Socket("127.0.0.1",4242);
InputStreamReader streamreader= new InputStreamReader(client.getInputStream());
BufferedReader reader = new BufferedReader(streamreader);

String reed = reader.readLine();
System.out.println(reed);
reader.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}

public static void main(String[] m) {
client d =new client();
d.go();
}
}

It compiles w/o any error,but when I try to run it I get the following output:
java.net.SocketException: Operation not permitted
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:430)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at java.net.Socket.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:178)
at client.go(client.java:9)
at client.main(client.java:28)

If I use sudo java client I get :

java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:430)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at java.net.Socket.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:178)
at client.go(client.java:9)
at client.main(client.java:28)


thanks for any suggestions
[ April 12, 2006: Message edited by: Stephan Norwood ]
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You try to connect to a port, no process is listening to that port and you get an exception. Sounds like the program is working correctly.
 
Stephan Norwood
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I wrote the following server code:

import java.io.*;
import java.net.*;

public class server {
public void go() {
try {
ServerSocket servsock = new ServerSocket(4242);
Socket sock= servsock.accept();
PrintWriter writer=new PrintWriter(sock.getOutputStream());
System.out.print("connection made \r\n");
writer.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}//close go
public static void main (String[] m) {
server myserver =new server();
myserver.go();
}
}//close class

I get the same java.net.SocketException: Operation not permitted
error.
When run the client with "sudo java client" I get the output :
connection made
null.

Where does this null come from,and why do I need superuser privileges to run my client ???
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephan Norwood:

Where does this null come from,

The line "System.out.println(reed);". What are you writing to the socket on the server side?

Originally posted by Stephan Norwood:

why do I need superuser privileges to run my client ???

I have no idea. I'm running your code on Gentoo Linux and have no problem with the client. Is this your box or do you have a server admin you can speak with?
BTW, end-of-line markers can get hairy with sockets. Read this
 
Stephan Norwood
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

actually I am using Mac with javac version 1.5.0_06.

System.out.println(reed);
I wanted to read from whatever server was running at this port;
it is obsolete code,I forgott to delete it.

Mistake on my part.


Maybe some of the underlying low level network methods require root privileges.

One point I am not getting is the difference between
a)System.out.println("blah blah");
b)writer.println("blah blah")

(writer is defined as PrintWriter writer ; )

as far as I understand :
a) sends the message to the screen and
b)sends the message to the server socket


and what does the following code tell me;
writer.println(outgoing.getText());
writer.flush();

(outgoing is a JTextField ; )

I know how to use flush() with buffers.

thanks
[ April 12, 2006: Message edited by: Stephan Norwood ]
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephan Norwood:

as far as I understand :
a) sends the message to the screen and
b)sends the message to the server socket

You are correct. If you look at the API documentation for System, you'll see that System.out is declared to be a PrintStream, so the similarity between the calls is no surprise.

Originally posted by Stephan Norwood:

and what does the following code tell me;
writer.println(outgoing.getText());
writer.flush();

getText() gets the contents of a text component as a String. writer.println will put that String into whatever data sink it is attached to (in your case a Socket) and append a platform-dependent line terminator to it.
 
Stephan Norwood
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your reply.

so I use writer.flush() ,because writer is created without automatic line flushing??
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.
 
Stephan Norwood
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK got it.
It is not yet clear to me what if I don't flush writer?
Do I always have to flush a PrintWriter object or are there cases when I don't have to?

[ April 12, 2006: Message edited by: Stephan Norwood ]
[ April 12, 2006: Message edited by: Stephan Norwood ]
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data has to run through several layers in order to get to its destination. There can be buffering at the application layer (i.e. BufferedReader), the OS layer and the hardware layer (i.e. hard drive cache) and anywhere between. If you don't flush the Stream/Writer, the data may still be cached somewhere when close() is called on the Stream/Writer and never get written.
Look at the API Documentation for the particulars of each class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!