aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Simple program for URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Simple program for URL" Watch "Simple program for URL" New topic
Author

Simple program for URL

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi All,
I wrote following program:import java.net.*;
import java.io.*;
public class MyTest1 {
URL url;
BufferedReader in;
PrintWriter out;
String s;
public MyTest1()
{
try
{
URL url = new URL("http://10.3.158.172:80");
}
catch(MalformedURLException e)
{
System.out.println("Connection failed");
}
try
{
URLConnection uc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine;
while (true){
inputLine = in.readLine();
if(inputLine == null) break;
out.println(inputLine);
System.out.println(in.readLine());
}
}
catch(IOException e)
{
System.out.println("Here is an error" + e.getMessage());
}
}
public static void main (String[] args){
MyTest1 t = new MyTest1();
}
}
Where I have to run this program means Client or Server?

Please let me know
Thanks,
Angel
maateen
Greenhorn

Joined: Dec 18, 2000
Posts: 23
hello,
U have to run ur server program first in one java console then run client program in new console
some points to remember...
ur server program should have to listen on a specific port in this program ur server should have to listen on port 80
but the more important thing is that ports above 1024 are for for system & are allocated automatically by Operating system.
so try to use port above 1024
here is some code which may help U
Sever Code
U may run it by changing the IP & port of ur desired machine
//________________________
import java.util.*;
import java.io.*;
import java.net.*;
public class SimpleChatServer extends Thread {
private static final String SERVER_MSG_PREFIX = "SERVER";
private static final String CLIENT_MSG_PREFIX = "GETNEXTMSG";
private static final String ECHO = "ECHO";
protected java.util.Vector connections=null;
private String str=null;
public static void main(String argv[]) {
new SimpleChatServer();
}
public SimpleChatServer(){
int port = 6006;
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
System.out.println("Server Socket Created Waiting for Clients...");
} catch (IOException e) {
System.out.println("Could not listen on port: " + port + ", " + e);
System.exit(1);
}
connections = new java.util.Vector();
System.out.println("Ready...");
while (true) {
Socket s = null;
try {
s = ss.accept();
System.out.println("Socket Created\n"+s);
}
catch (IOException e) {
System.out.println("Accept failed: " + port + ", " + e);
System.exit(1);
}
try {
BufferedReader read=new BufferedReader(new InputStreamReader(s.getInputStream()));
str = read.readLine();
System.out.println(str);
PrintWriter print=new PrintWriter(s.getOutputStream(),true);
print.println("Java Chat Service at port: 6006");
str = str.substring("GET /".length(),str.length()-" HTTP/1.1".length());
System.out.println(str);
if(str.startsWith(SimpleChatServer.CLIENT_MSG_PREFIX)){
synchronized(connections){
connections.addElement(new HttpConnection(s,s.getOutputStream(),s.getInputStream()));
}
System.out.println(connections);
continue;
}
if(str.startsWith(SimpleChatServer.ECHO)){
str = str.substring(SimpleChatServer.ECHO.length());
read.close();
print.close();
s.close();
for(int i = 0;i<connections.size();i++){>
HttpConnection connection = (HttpConnection)connections.elementAt(i);
synchronized(connections){
synchronized(connection){
connection.writeToConnection(str);
connection.writeClose();
connection.socketClose();
}
}
}
connections.removeAllElements();
System.out.println(connections);
continue;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple program for URL
 
Similar Threads
Networking question
http URL connection
URL and Severs(very Urgent)
url reader applet
How to read text content not source code from webpage in java ?