User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,058 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,269 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2887 | Replies: 12
Reply
Join Date: Jul 2005
Posts: 13
Reputation: gokul is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gokul gokul is offline Offline
Newbie Poster

Chatting Program - Throws IO Exception

  #1  
Jul 21st, 2005
Hi,

I am developing small chatting application...
when i try to run the application, it throws the IO Exception while creating sockets..

How to resolve resolve the this one..? please help me..

i attached the code for your view...
Attached Files
File Type: java ExChat.java (3.7 KB, 19 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Chatting Program - Throws IO Exception

  #2  
Jul 21st, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 13
Reputation: gokul is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gokul gokul is offline Offline
Newbie Poster

Re: Chatting Program - Throws IO Exception

  #3  
Jul 21st, 2005
Hai Server_crash,
What is the another way to obtain that..?
give me a solution...
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Chatting Program - Throws IO Exception

  #4  
Jul 21st, 2005
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
Reply With Quote  
Join Date: Jul 2005
Posts: 13
Reputation: gokul is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gokul gokul is offline Offline
Newbie Poster

Re: Chatting Program - Throws IO Exception

  #5  
Jul 22nd, 2005
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..

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 7:42 pm.
Reply With Quote  
Join Date: Jul 2005
Posts: 56
Reputation: Sauce is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

Re: Chatting Program - Throws IO Exception

  #6  
Jul 22nd, 2005
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?
Reply With Quote  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Chatting Program - Throws IO Exception

  #7  
Jul 24th, 2005
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
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
Reply With Quote  
Join Date: Feb 2008
Posts: 1
Reputation: lijumon is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lijumon lijumon is offline Offline
Newbie Poster

Re: Chatting Program - Throws IO Exception

  #8  
Feb 15th, 2008
Can any one help me with a solution to use porttalk.sys to communicate with parallel port

Thank You
Reply With Quote  
Join Date: Mar 2004
Posts: 715
Reputation: Phaelax is on a distinguished road 
Rep Power: 6
Solved Threads: 28
Phaelax Phaelax is offline Offline
Master Poster

Re: Chatting Program - Throws IO Exception

  #9  
Feb 15th, 2008
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.
Reply With Quote  
Join Date: Jul 2008
Posts: 22
Reputation: andrew13d is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
andrew13d andrew13d is offline Offline
Newbie Poster

Re: Chatting Program - Throws IO Exception

  #10  
31 Days Ago
hey all,
I was also thinking about a chatting application. However I am kinda new to programming. Would someone pliz guide me on how to go about it using C++ or C#(i've gone through the basics of C++). Can the chatting "operation" be achieved by scripting also?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC