Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Simple program for URL

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
}

 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic