This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-12-29 02:57:34

Mcsugarface
Scratcher
Registered: 2009-11-16
Posts: 100+

How to use Java Sockets

How do I use Sockets in Java? The Oracle tutorial is confusing.


Hi! I'm http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=onlineStatus&type=text!
http://internetometer.com/imagesmall/11070.png

Offline

 

#2 2011-12-29 03:23:05

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: How to use Java Sockets

http://zerioh.tripod.com/ressources/sockets.html

Offline

 

#3 2011-12-29 03:25:29

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: How to use Java Sockets

Code:

String host = "127.0.0.1";//the host you want to connect to
into port = 42001;//the port you want to connect on
Socket socket = new Socket(host,  port);//connect!

it you want to read and write to the socket just ask!


http://i.imgur.com/1QqnHxQ.png

Offline

 

#4 2011-12-29 15:28:58

Mcsugarface
Scratcher
Registered: 2009-11-16
Posts: 100+

Re: How to use Java Sockets

TRocket wrote:

Code:

String host = "127.0.0.1";//the host you want to connect to
into port = 42001;//the port you want to connect on
Socket socket = new Socket(host,  port);//connect!

it you want to read and write to the socket just ask!

Thanks! How do I read/write?


Hi! I'm http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=onlineStatus&type=text!
http://internetometer.com/imagesmall/11070.png

Offline

 

#5 2011-12-29 15:42:05

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: How to use Java Sockets

Code:

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write("Hello, World!"); //Writes "Hello, World!" to the stream

big_smile

Last edited by MathWizz (2011-12-29 15:42:49)


http://block.site90.net/scratch.mit/text.php?size=30&text=%20A%20signature!&color=333333

Offline

 

#6 2011-12-29 15:47:06

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: How to use Java Sockets

Mcsugarface wrote:

TRocket wrote:

Code:

String host = "127.0.0.1";//the host you want to connect to
into port = 42001;//the port you want to connect on
Socket socket = new Socket(host,  port);//connect!

it you want to read and write to the socket just ask!

Thanks! How do I read/write?

Code:

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream));
PrintWriter out = new PrintWriter(socket.getOutputStream);
//read a single line
in.readLine();//returns a string
//writing ,to use readline to read this be sure not to miss the \n !
out.write("message goes here\n");
out.flush();//send it!

ps if you are trying to connect to scratch it may be easier to use the one I've already made. it's in the konnected thread.in AT

Last edited by TRocket (2011-12-29 15:51:12)


http://i.imgur.com/1QqnHxQ.png

Offline

 

Board footer