473,803 Members | 4,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

query to website

35 New Member
i am writing an client application that connects to the server and then sends data to server.
but, the problem is that i get a java.net.Connec tException: Connection refused error.
The code i have written is :
Expand|Select|Wrap|Line Numbers
  1. //package bdn;
  2. /*  The java.net package contains the basics needed for network operations. */
  3. import java.net.*;
  4. /* The java.io package contains the basics needed for IO operations. */
  5. import java.io.*;
  6. /** The SocketClient class is a simple example of a TCP/IP Socket Client.
  7.  *
  8.  */
  9.  
  10. public class SocketClient {
  11.      public static void main(String[] args) {
  12.         /** Define a host server */
  13.         //find the ip address by ping cmd in unix
  14.         String host = "211.65.63.147";
  15.         /** Define a port */
  16.         int port = 8080;
  17.  
  18.         StringBuffer instr = new StringBuffer();
  19.         String TimeStamp;
  20.         System.out.println("SocketClient initialized");
  21.  
  22.         try {
  23.           /** Obtain an address object of the server */
  24.           InetAddress address = InetAddress.getByName(host);
  25.           /** Establish a socket connetion */
  26.           Socket connection = new Socket(address, port);
  27.           /** Instantiate a BufferedOutputStream object */
  28.           BufferedOutputStream bos = new BufferedOutputStream(connection.
  29.               getOutputStream());
  30.  
  31.           /** Instantiate an OutputStreamWriter object with the optional character
  32.            * encoding.
  33.            */
  34.           OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
  35.  
  36.         }
  37.         catch (Exception g) {
  38.         System.out.println("Exception: " + g);
  39.             }
  40.  
  41.     }
  42.  
  43.  
  44. }
  45.  
please let me know wat could be the problem.
Jan 24 '09 #1
13 3036
JosAH
11,448 Recognized Expert MVP
Why don't you use a URL and a URLConnection for this purpose?

kind regards,

Jos
Jan 24 '09 #2
eternalLearner
35 New Member
hmm ok,
but i wrote this code using url,but even this doesnot work ?
import java.io.Buffere dReader;
import java.io.InputSt ream;
import java.io.InputSt reamReader;
import java.net.URL;

public class Sample1 {

public static void main(String[] args) {

try {
//String url =
// "https://services.nature serve.org/" +
// "idd/rest/ns/v1.1/globalSpecies/comprehensive";
String url = "http://www.bioinf.seu. edu.cn/miRNA/index.html";
String uid = "ELEMENT_GLOBAL .2.100925";
String nsAccessKeyId = "72ddf45a-c751-44c7-9bca-8db3b4513347";
String Query = "seqences=/>Example \nCUUGGGAAUGGCA AGGAAACCGUUACCA UUACUGAGUUUAGUA AUGGUAAUGGUUCUC UUGCUAUACCCAGA" ;


String request = url;
request = request + "?";
//request = request + "uid=" + uid;
//request = request + "&";
//request = request + "NSAccessKeyId= " + nsAccessKeyId;
request = request + Query;

URL serviceURL = new URL(request);
InputStream is = serviceURL.open Stream();
//InputStreamRead er isr = new InputStreamRead er(is,"UTF-8");
InputStreamRead er isr = new InputStreamRead er(is);
BufferedReader br = new BufferedReader( isr);

StringBuffer response = new StringBuffer();
String nextLineFromSer vice = br.readLine();
while (nextLineFromSe rvice != null) {
response.append (nextLineFromSe rvice);
nextLineFromSer vice = br.readLine();
}

System.out.prin tln(response);
}
catch (Exception e) {
System.out.prin tln(e);
}

}
}
Jan 27 '09 #3
JosAH
11,448 Recognized Expert MVP
I can't contact that site either (using my browser). Are you sure the url address is correct?

kind regards,

Jos
Jan 28 '09 #4
eternalLearner
35 New Member
the site is down i guess.
earlier i was able to connect to the website using browser.
but ,do u think my program is correct ?
Jan 28 '09 #5
JosAH
11,448 Recognized Expert MVP
@eternalLearner
Try it with a simple url, e.g. http://www.google.com and see what happens.

kind regards,

Jos
Jan 28 '09 #6
chaarmann
785 Recognized Expert Contributor
@eternalLearner
Yes, it is.
But you can enhance it. This is what I am using:

Expand|Select|Wrap|Line Numbers
  1.    String url = "http://www.google.com";
  2. StringBuilder response = new StringBuilder(10000);
  3.    BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
  4.    String line;
  5.    while ((line = reader.readLine()) != null) response.append(line);
  6.    String html = response.toString();
  7.  
BufferedReader is faster than StringBuffer. You should use StringBuffer only in multithreaded environment (what you don't have), and if you need to synchronize.

Also note that if you call "s= new StringBuffer()" or
"s=new StringBuilder() ", the initial capacity is only 16 characters, so it will need to allocate memory many times to hold an average response of around 7000 bytes in my case. Memory allocation itself is very costly, independent of how many memory you allocate (if it's not some megabytes).
So just estimate the maximum size of your returned webpage (in my case 10000) and use that for initial capacity.
Jan 29 '09 #7
JosAH
11,448 Recognized Expert MVP
@chaarmann
It isn't; in Java object allocation is no more than a test, an increment of a pointer and the return of the old pointer value. It isn't like a C-like malloc or a C++-like new operator. Garbage collection can be expensive.

kind regards,

Jos

ps. There's also the StringBuilder class that doesn't synchronize on its buffer.
Jan 29 '09 #8
chaarmann
785 Recognized Expert Contributor
@JosAH
But isn't there a difference between memory allocation and object allocation in Java? I mean to allocate a new object, you can just re-use an old unused object by modifying the pointer (or pointing to a re-usable template if you keep object structure and its data structure separate). But memory allocation goes down deep into the operation system, from chip-cache to first-level cache to second level cache and so on up to saving memory to disk if there is no space available etc. Usually if you need more memory then you need to copy the whole string into a new string. You cannot just increase a pointer and keep the string, because there is usually already some other data allocated at the end of this string. I admit that I don't know how Java handles memory and I know it well from C++. So how should that be possible in Java theoretically? I thought this problem is universal.
Also I know that if you allocate small amounts of memory many times, you get memory fragmentation. Then you need to search through the memory map to find a suitable gap. Assuming the average returned html page is 7000 characters long, then you have to find a new gap 9 times:
16 --> 32 --> 64 --> 128 --> 256 --> 512 --> 1024 --> 2048 --> 4096 --> 8192
Don't you think that's costly?

@JosAH
That's what I wanted to express. It's good that you pinpointed this error in my previous post. I wrote: "BufferedRe ader is faster than StringBuffer",
but wanted to write: "StringBuilder is faster than StringBuffer". That's why I used it in my program.
Quotation from Javadocs, StringBuffer class:
"As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread,StringBu ilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization . "
Jan 29 '09 #9
chaarmann
785 Recognized Expert Contributor
I read an interesting article about java memory allocation, especially about the generational garbage collector.
Quotation: "allocating a new object is nothing more than checking to see if there's enough space left in the heap and bumping a pointer if there is. No searching free lists, best-fit, first-fit, lookaside lists -- just grab the first N bytes out of the heap and you're done."

But doesn't that involve copying the old string to the new location every time?
Jan 29 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2278
by: leegold2 | last post by:
Let's I do a mysql query and then I do a, for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...} and it gives me this: PageID Title URL Description 1 lee's gogle1.com This is lee's website. 1 lee's gogle2.com This is lee's website. 2 Jon's yaho1.com This is Jon's website. 2 Jon's yaho2.com This is Jon's website.
14
3236
by: Bruce W...1 | last post by:
I do a query to MySQL using PHP. Well actually I do too many of them which is the problem, it's too slow. I think maybe an inner join or something would be better but I can't figure this out. The web page on which the data is displayed has a list of countries. Each country has a label heading under which websites are listed that have information on that country. Any given website may appear many times (under different countries) if it...
1
1374
by: Lukelrc | last post by:
Hi. I have a table (websitehits) which holds statistics about websites. This table has a date field (datecounted). What I need is to create a query which returns a list of dates between two date ranges (say a year ago from today and a year from now) which only shows dates that haven't been used in the websitehits table for a given website. For example if my table contains something like:
2
1458
by: tdmailbox | last post by:
I have a database with three tables tbl_listings - listings of houses on for sale tbl_intersted - table which tracks if a user is interested in the listing, it has two columns mls(the key for tbl_listings) and user(user login) tbl_review - table which trackes if a user has reviewed the listing. Like tbl_interested it has two columns (the key for tbl_listings) and user(user login)
3
2837
by: MX1 | last post by:
Here's a type of query I'm always having trouble with. There are 4 fields in a web access log table. User | Date | Time | Destination I want to show all the instances of a user hitting a particular web site, but I only want a single entry to represent that visit (not the 10 or 15 that represent every graphic that was obtained from the page as is typical with imported web logs).
4
1968
by: Wim Verhavert | last post by:
Hi all, For gathering certain information in building my database, I'm depending on the things I found on the web, especially one website (let's call it www.mywebsite.com). Is there a way to query this site (it has fill in boxes on the page and a search button) depending on values in my form and getting the results back in my form? This (general) question is keeping me out of my sleep for weeks now.... Any tip is appreciated....
9
1735
by: Jeff Gardner | last post by:
Greetings: I have an UPDATE query (php 5.1.6/mysql 5.0.24a on apache 2.2) that appears to execute with no errors (php,mysql, or apache) but the data in the "UPDATED" table doesn't change. I've checked privileges for connecting account and that isn't an issue. Maybe it's my query: $editO = " UPDATE organization SET
9
4026
by: JJM0926 | last post by:
I'm trying to create a running totals query in access 97. I have followed the directions on how to do it from Microsofts website article id 138911. I took their code they had and replaced it with my fields. When I try to run it I get #errors in my RunTot column. I'm kinda new to this. Not sure if maybe I mistyped something wrong or is there a better way to do this? I have pasted the code. Any help would be greatly appreciated....
8
2449
siridyal
by: siridyal | last post by:
I have a wholesale website that i'm working on that shows hundreds of items that are updated from time to time. These items are kept in a mysql database with several tables. I want to let the the customer browse (from a dynamically created drop down list - which i've done) the items by furniture class, and then have the query results limited to 26 items per page, in two columns using DIVs. The CSS for the DIVs is taken care of, as is the...
3
4834
by: DigitalWallfare | last post by:
Hi all, This is my first post here, but i've lurked for a while. I'm working on a website but have come across a major stumbling block in the code: I've managed to structure the search query but have 2 problems: I dont know how to code the results into the table, and make the table repeat itself 5 times per page (and recognise it needs a new page)
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.