473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating java server problem

270 100+
Hello friends, actually i have a problem with following programs:
First:---------
import java.net.*;
import java.io.*;
public class SimpleServer{
public static void main(String[] a){
ServerSocket s=null;
try{
s=new ServerSocket(5432);
}catch(IOException e){
e.printStackTrace();
}
while(true){
try{
Socket s1=s.accept();
OutputStream s1out=s1.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s1out));
bw.write("Hello Net World!!");
bw.close();
s1.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}

Second:----------
import java.net.*;
import java.io.*;

public class SimpleClient {
public static void main(String[] a) {
try
{
Socket s1 = new Socket("127.0.0.1", 5432);
InputStream is = s1.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
br.close();
s1.close();
}
catch (ConnectException con)
{
System.err.println("Coult not connect");
}
catch (IOException ex)
{
//ex.printStackTrace();
}
}
}

when i compile and run these programs in two different cmd windows nothing is shown as output, pls help!!
Apr 3 '08 #1
5 1806
JosAH
11,448 Expert 8TB
Better use a (Buffered)Reader on the client side because the server side uses
a BufferedWriter for the printing purposes. A DataInputStream is normally used
for reading binary data.

kind regards,

Jos
Apr 3 '08 #2
NitinSawant
270 100+
thanx bro, can you pls tell me how to use that reader???
Apr 3 '08 #3
JosAH
11,448 Expert 8TB
thanx bro, can you pls tell me how to use that reader???
Simply read the API documentation. A BufferedReader even has a readLine()
method. If you use a PrintWriter on the server side all will be fine.

kind regards,

Jos
Apr 3 '08 #4
r035198x
13,262 8TB
thanx bro, can you pls tell me how to use that reader???
Similar to how you have done the writing with the BufferedWriter above. This time you'll be reading so use BufferedReader.

P.S Use code tags when posting code
P.P.S Nice avatar
Apr 3 '08 #5
NitinSawant
270 100+
thanx a lot r035198x, JosAH
I've modified it and it is working fine ..

Expand|Select|Wrap|Line Numbers
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class SimpleClient {
  5.     public static void main(String[] a) {
  6.         try
  7.         {
  8.             Socket s1 = new Socket("127.0.0.1", 5432);
  9.             InputStream is = s1.getInputStream();
  10.             BufferedReader rdr=new BufferedReader(new InputStreamReader(is));
  11.             String str=rdr.readLine();
  12.             System.out.println(str);
  13.             rdr.close();
  14.             s1.close();
  15.         }
  16.         catch (ConnectException con)
  17.         {
  18.             System.err.println("Coult not connect");
  19.         }
  20.         catch (IOException ex)
  21.         {
  22.             //ex.printStackTrace();
  23.         }
  24.     }
  25. }
  26.  
Apr 3 '08 #6

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

Similar topics

0
by: Rennie deGraaf | last post by:
I want to create a long-running RMI server, which makes an object available to remote hosts, and does nothing else. However, the server consistently exists with no exception or error after >=60...
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
2
by: rhen18 | last post by:
I'm using a NoMachine - NX Client for Windows and when I log in, I get a "connection error" ( thats what exactly is displayed) Here are the details of the error. NX> 203 NXSSH running with pid:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.