File: SocketTest.java

package info (click to toggle)
waba 1.5-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 1,996 kB
  • ctags: 3,231
  • sloc: ansic: 17,303; java: 4,436; sh: 2,345; makefile: 417
file content (54 lines) | stat: -rw-r--r-- 1,210 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
  SocketTest.java

  This is to demonstrate the functionality of Socket.class.

  This program
  0. connects to the port 80 of www.yahoo.com and 
  1. writes "GET / HTTP/1.0\n\n" and
  2. reads 1024 bytes and dumps them to a file called tmp.

  To run SocketTest, you need to use the application mode.
  Example: waba --as-application SocketTest

 */

import waba.io.Socket;
import waba.io.File;

public class SocketTest
{
	public static void main (String[] args)
	{
		Socket socket = new Socket ("www.yahoo.com", 80);
		File file = new File ("tmp", File.CREATE);
		byte get[] = { (byte)'G', (byte)'E', (byte)'T',
			(byte)' ', (byte)'/', (byte)' ', (byte)'H',
			(byte)'T', (byte)'T', (byte)'P', (byte)'/',
			(byte)'1', (byte)'.', (byte)'0', (byte)'\n',
			(byte)'\n' };

		byte b[] = new byte[1024];

		if (file.isOpen() && socket.isOpen())
		{
			int count;
			socket.writeBytes(get, 0, get.length);
			count = socket.readBytes(b, 0, 1024);
			if (count > 0)
			{
				file.writeBytes(b, 0, 1024);
				file.close();
			} else {
				b[0] = (byte) 'n';
				b[1] = (byte) 'o';
				b[2] = (byte) '!';
				b[3] = (byte) '\n';
				file.writeBytes(b, 0, 4);
				file.close();
			}
			socket.close();
		}

	}
}