By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

java networking

P: 9
hi,
i just want 2 communicate with the near by computer. i got this program from internet. i have two programs called Server.java and Client.java.

i am putting that program here now,
Server.java Program:

import java.net.*;
import java.io.*;

public class Server
{
public static void main(String[] args) throws IOException{
Socket client= null;
PrintWriter pout=null;
ServerSocket sock=null;

try{
sock= new ServerSocket(5155);
// now listen for connections

while(true){
client= sock.accept();

// we have a connection
pout = new PrintWriter(client.getOutputStream(), true);

// write the date to the socket
pout.println(new java.util.Date().toString());

pout.close();
client.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally {
if (client !=null)
client.close();
if (sock !=null)
sock.close();
}
}
Client.java Program:

import java.net.*;
import java.io.*;

public class Client
{
public static void main(String[] args) throws IOException{
InputStream in = null;
BufferedReader bin = null;
Socket sock = null;

try {
// make connection to socket
sock = new Socket("127.0.0.1",5155);

in = sock.getInputStream();
bin = new BufferedReader(new InputStreamReader(in));

String line;
while ((line= bin.readLine())!=null)
System.out.println(line);
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally{
if (sock !=null)
sock.close();
}
}
}

tell me wat shud i change in this program?
is it correct doing this?
Help me in doing this?
its urgent



}
Jul 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: 1
hi,
i just want 2 communicate with the near by computer. i got this program from internet. i have two programs called Server.java and Client.java.

i am putting that program here now,
Server.java Program:

import java.net.*;
import java.io.*;

public class Server
{
public static void main(String[] args) throws IOException{
Socket client= null;
PrintWriter pout=null;
ServerSocket sock=null;

try{
sock= new ServerSocket(5155);
// now listen for connections

while(true){
client= sock.accept();

// we have a connection
pout = new PrintWriter(client.getOutputStream(), true);

// write the date to the socket
pout.println(new java.util.Date().toString());

pout.close();
client.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally {
if (client !=null)
client.close();
if (sock !=null)
sock.close();
}
}
Client.java Program:

import java.net.*;
import java.io.*;

public class Client
{
public static void main(String[] args) throws IOException{
InputStream in = null;
BufferedReader bin = null;
Socket sock = null;

try {
// make connection to socket
sock = new Socket("127.0.0.1",5155);

in = sock.getInputStream();
bin = new BufferedReader(new InputStreamReader(in));

String line;
while ((line= bin.readLine())!=null)
System.out.println(line);
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally{
if (sock !=null)
sock.close();
}
}
}

tell me wat shud i change in this program?
is it correct doing this?
Help me in doing this?
its urgent



}
This program is correct!
At first you must compile and run Server class and than Client
Jul 27 '06 #2

P: 9
it is throwing me ConnectionException error

i dont no wat 2 do 4 this?
Jul 27 '06 #3

P: 9
hiiii,

where shud i compile this?

Computer1: server.java
Computer2: Client.java. In this i am giving the IP address of that Server.

In which computer i shud compile this?

tell me . its urgent.
Jul 28 '06 #4

P: 1
You must look there must be same port (number), and same IP address to connect ot server. I'll give you another version. I have Net3.java and Net4.java. And I'll bring tomorrow.
Aug 8 '06 #5

Post your reply

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