Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 3359 | Replies: 12
![]() |
•
•
•
•
| |
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
That means it couldn't find the destination. I noticed you are using command line args. Do you know how to get those? It won't run unless you have an argument to the right destination.
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
Another way would be using a bufferedreader to get input from the user. But why not jut compile with the correct arguments? You know how to do that right?
javac ProgramName.java
java ProgramName argument1
javac ProgramName.java
java ProgramName argument1
•
•
Join Date: Jul 2005
Posts: 13
Reputation:
Rep Power: 4
Solved Threads: 0
Hai,
did u watch my code..?
In that code, everything is fine and correct..
i am running by this way,
C:\Java>java ExChat aik6 100
Here,
ExChat - my Pgm Name
aik6 - my system name..
100 - the port for my socket
but when i run i got the error message...
"IO Exception Error for Creating Socket"..
it doesn't take my system name..thats the problem...
i want to chat with in the same system...
how i modify code...
here, i pasted my code..
And another one, how i get my ip address and place in the InetAddress...?
pls help me...
Regards,
Gokulakrishnan.M :-|
did u watch my code..?
In that code, everything is fine and correct..
i am running by this way,
C:\Java>java ExChat aik6 100
Here,
ExChat - my Pgm Name
aik6 - my system name..
100 - the port for my socket
but when i run i got the error message...
"IO Exception Error for Creating Socket"..
it doesn't take my system name..thats the problem...
i want to chat with in the same system...
how i modify code...
here, i pasted my code..
import java.lang.System;
import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.*;
public class ExChat {
public static void main(String args[]){
PortTalk portTalk = new PortTalk(args);
portTalk.displayDestinationParameters();
portTalk.displayLocalParameters();
portTalk.chat();
portTalk.shutdown();
}
}
class PortTalk {
Socket connection;
DataOutputStream outStream;
BufferedReader inStream;
public PortTalk(String args[]){
if(args.length!=2) error("Usage: java PortTalkApp host port");
String destination = args[0];
int port = 0;
try {
port = Integer.valueOf(args[1]).intValue();
}catch (NumberFormatException ex){
error("Invalid port number");
}
try{
connection = new Socket(destination,port);
}catch (UnknownHostException ex){
error("Unknown host");
}
catch (IOException ex){
error("IO error creating socket");
}
try{
inStream = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
outStream = new DataOutputStream(connection.getOutputStream());
}catch (IOException ex){
error("IO error getting streams");
}
System.out.println("Connected to "+destination+" at port "+port+".");
}
public void displayDestinationParameters(){
InetAddress destAddress = connection.getInetAddress();
String name = destAddress.getHostName();
byte ipAddress[] = destAddress.getAddress();
int port = connection.getPort();
displayParameters("Destination ",name,ipAddress,port);
}
public void displayLocalParameters(){
InetAddress localAddress = null;
try{
localAddress = InetAddress.getLocalHost();
}catch (UnknownHostException ex){
error("Error getting local host information");
}
String name = localAddress.getHostName();
byte ipAddress[] = localAddress.getAddress();
int port = connection.getLocalPort();
displayParameters("Local ",name,ipAddress,port);
}
public void displayParameters(String s,String name,
byte ipAddress[],int port){
System.out.println(s+"host is "+name+".");
System.out.print(s+"IP address is ");
for(int i=0;i<ipAddress.length;++i)
System.out.print((ipAddress[i]+256)*256+".");
System.out.println();
System.out.println(s+"port number is "+port+".");
}
public void chat(){
BufferedReader keyboardInput = new BufferedReader(
new InputStreamReader(System.in));
boolean finished = false;
do {
try{
System.out.print("Send, receive, or quit (S/R/Q): ");
System.out.flush();
String line = keyboardInput.readLine();
if(line.length()>0){
line=line.toUpperCase();
switch (line.charAt(0)){
case 'S':
String sendLine = keyboardInput.readLine();
outStream.writeBytes(sendLine);
outStream.write(13);
outStream.write(10);
outStream.flush();
break;
case 'R':
int inByte;
System.out.print("***");
while ((inByte = inStream.read()) != '\n')
System.out.write(inByte);
System.out.println();
break;
case 'Q':
finished=true;
break;
default:
break;
}
}
}catch (IOException ex){
error("Error reading from keyboard or socket");
}
} while(!finished);
}
public void shutdown(){
try{
connection.close();
}catch (IOException ex){
error("IO error closing socket");
}
}
public void error(String s){
System.out.println(s);
System.exit(1);
}
}And another one, how i get my ip address and place in the InetAddress...?
pls help me...
Regards,
Gokulakrishnan.M :-|
Last edited by cscgal : May 3rd, 2006 at 8:42 pm.
•
•
Join Date: Jul 2005
Posts: 56
Reputation:
Rep Power: 4
Solved Threads: 0
Ok so its 9 am...and still no sleep so i wont go thru all this code but just a quick little two cents worth. Its a chat program, the way i normally do these (and there might be others) is to use 2 ports (send / receive) and multiple threads. 1 thread for gui, 1 thread for functionality, and 1 thread for a "ConnectionListener".
The program that is listening for a connection (chat client to connect) needs to create a ServerSocket. e.g. "ServerSocket myServerSocket = new ServerSocket( << a port # >>);"
That port is now locked on your system so unless you start a client (with listener) on a different port you will encounter problems (or at least i did when i wrote my first chat program).
A quick run thru on how i believe ServerSockets work. They listen on a port and when a connection is attempted with "Socket mySocket = MyServerSocket.accept();" it makes a connection (socket) on a port (other then the one being listen upon). Thus if you want to accept multiple connection. e.g. chat with more then 1 person at a time put the code to accept connections in a loop to keep on listening for more connections.
if you dont get this fixed i'll take a look at your code after i get some sleep.
p.s. does "InetAdress.getLocalHost().getHostAddress()" accomplish what you needed for the ip?
The program that is listening for a connection (chat client to connect) needs to create a ServerSocket. e.g. "ServerSocket myServerSocket = new ServerSocket( << a port # >>);"
That port is now locked on your system so unless you start a client (with listener) on a different port you will encounter problems (or at least i did when i wrote my first chat program).
A quick run thru on how i believe ServerSockets work. They listen on a port and when a connection is attempted with "Socket mySocket = MyServerSocket.accept();" it makes a connection (socket) on a port (other then the one being listen upon). Thus if you want to accept multiple connection. e.g. chat with more then 1 person at a time put the code to accept connections in a loop to keep on listening for more connections.
if you dont get this fixed i'll take a look at your code after i get some sleep.
p.s. does "InetAdress.getLocalHost().getHostAddress()" accomplish what you needed for the ip?
•
•
Join Date: Jun 2004
Posts: 604
Reputation:
Rep Power: 6
Solved Threads: 6
Hi everyone,
Your error is due to the fact that you have no server. Read up on sockets and serversockets to improve your skills
Richard West
Your error is due to the fact that you have no server. Read up on sockets and serversockets to improve your skills
Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
•
•
Join Date: Mar 2004
Posts: 733
Reputation:
Rep Power: 6
Solved Threads: 32
lijumon, make your own thread and do not attempt to hijack other threads.
gokul, what is "aik6 - my system name"? It might know how to connect to 'localhost' or an IP address, but unless your home network is setup to recognize aik6 in that manner it probably won't know what the heck that address is.
gokul, what is "aik6 - my system name"? It might know how to connect to 'localhost' or an IP address, but unless your home network is setup to recognize aik6 in that manner it probably won't know what the heck that address is.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Hybrid Mode