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...

Attachments
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);		
}		
}

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.

Hai Server_crash,
What is the another way to obtain that..?
give me a solution...

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

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 :-|

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?

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

Can any one help me with a solution to use porttalk.sys to communicate with parallel port

Thank You

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.

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?

Well, you are off to a good start by choosing to hijack an old thread in the Java forum to ask someone to walk you through it.

Post your question in the C++ or C# forum and don't be surprised if you get very general answers to such a general question.

Thanks. I will try that but about the question being so general I don't really know how specific I can make it, but I will try anyway.

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?

If you want to do chat application in C++ or C# you should ask this question in appropriate section of our forum
C++ here
C# here

This article has been dead for over six months. Start a new discussion instead.