Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Sockets and Internet Protocols
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Sockets and Internet Protocols
My client server application is showing connection reset?
Parshant Sehrawat
Greenhorn
Posts: 4
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is my ChatServer class
import java.net.*; import java.util.Scanner; import java.io.*; public class ChatServer { private Socket socket = null; private ServerSocket server = null; private DataInputStream streamIn = null; private DataOutputStream streamOut = null; private FileInputStream read = null; private DataInputStream readFile = null; private Scanner in=null; public ChatServer(int port) { try { System.out.println("Binding to port " + port + ", please wait ..."); server = new ServerSocket(port); System.out.println("Server started: " + server); System.out.println("Waiting for a client ..."); socket = server.accept(); System.out.println("Client accepted: " + socket); open(); String msg=""; boolean done = false; int choice=0; while (!done) { choice=streamIn.readInt(); switch(choice){ case 1: //chat server String line="hi"; while(!((line.equals("bye") || msg.equals("bye")))){ try{ if((line = streamIn.readUTF())!=null) System.out.print("Client > "+line+"\n"); System.out.println("Server > "); msg=in.nextLine(); streamOut.writeUTF(msg); streamOut.flush(); done = (line.equals("bye") || msg.equals("bye")); } catch(IOException ioe){ done = true; } } break; case 2://file reader //streamOut.writeUTF("Enter The File Name : "); String fileName = streamIn.readUTF(); fileOpen(fileName); String content=""; while((content = readFile.readUTF())!= null){ streamOut.writeUTF(content); } streamOut.flush(); break; case 3: //multiplication method streamOut.writeUTF("Enter two integers : "); int op1=streamIn.readInt(),op2=streamIn.readInt(); streamOut.writeInt(op1*op2); streamOut.flush(); break; case 4: done=true; break; } } close(); } catch(IOException ioe){ System.out.println(ioe); } } private void fileOpen(String name) throws IOException{ name="C:\\Documents and Settings\\parshant.s\\workspace\\ChatServer\\" + name; try{ readFile=new DataInputStream( new FileInputStream("C:\\Documents and Settings\\parshant.s\\workspace\\ChatServer\\demo.txt")); } catch(Exception e){ streamOut.writeUTF("File not Found !!! Sorry"); } } public void open() throws IOException{ streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream())); streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); in=new Scanner(System.in); } public void close() throws IOException { if (socket != null) socket.close(); if (streamIn != null) streamIn.close(); } public static void main(String args[]) { ChatServer server = null; server = new ChatServer(5556); } }
Here is my ChatClient class
import java.net.*; import java.util.Scanner; import java.io.*; public class ChatClient { private Socket socket = null; private DataInputStream streamIn = null; private DataOutputStream streamOut = null; private Scanner in = null; public ChatClient(String serverName, int serverPort) { System.out.println("Establishing connection. Please wait ..."); try { socket = new Socket(serverName, serverPort); System.out.println("Connected: " + socket); } catch(UnknownHostException uhe) { System.out.println("Host unknown: " + uhe.getMessage()); } catch(IOException ioe) { System.out.println("Unexpected exception: " + ioe.getMessage()); } String line = "hi",msg="hi"; boolean done=false; while (!done) { try { start(); System.out.println("*************************************************"); System.out.println(" Server Menu "); System.out.println("*************************************************"); System.out.println(" 1. Chat to server "); System.out.println(" 2. Read a File "); System.out.println(" 3. Multiply two digits "); System.out.println(" 4. Exit "); System.out.println("*************************************************"); System.out.print(" Enter your choice : "); int choice = in.nextInt(); try { streamOut.writeInt(choice); } catch(Exception e){System.out.println("not able to write");} switch(choice){ case 1: while(!(line.equals("bye") || msg.equals("bye"))){ System.out.println("Client > "); line = in.nextLine(); streamOut.writeUTF(line); msg=streamIn.readUTF(); if(msg.length()>0) System.out.println("Server > "+msg); streamOut.flush(); } break; case 2: System.out.println("ghfjgu"+choice); System.out.print("Enter a file name : "); try{streamOut.writeUTF(in.next()); } catch(Exception e){ System.out.println("Hell!!!"); } String content=""; try{ while((content= streamIn.readUTF())!=null){ System.out.println(content); } } catch(Exception e){ System.out.println("Hell 2!!!!"); } streamOut.flush(); break; case 3: System.out.print(streamIn.readUTF()); streamOut.writeInt(in.nextInt()); streamOut.writeInt(in.nextInt()); System.out.println(" Result after multiplication is "+streamIn.readInt()); streamOut.flush(); break; case 4: done=true; break; } } catch(IOException ioe) { System.out.println("Sending error: " + ioe.getMessage()); } } } public void start() throws IOException { streamIn = new DataInputStream(socket.getInputStream()); streamOut = new DataOutputStream(socket.getOutputStream()); in=new Scanner(System.in); } public void stop() { try { if (in != null) in.close(); if (streamOut != null) streamOut.close(); if (socket != null) socket.close(); } catch(IOException ioe) { System.out.println("Error closing ..."); } } public static void main(String args[]) { ChatClient client = null; client = new ChatClient("10.10.1.61", 5556); } }
code is throwing many exception while reading a file or multiply things.
Can any one help me regarding this?
Sucheta Shrivastava
Ranch Hand
Posts: 151
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
why have you included a try catch at line : 39 to 43
try
{
streamOut.writeInt(choice);
}
catch(Exception e){System.out.println("not able to write");}
what does it do
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
TCP Threaded Server Client Question
ObjectOutputStream thread issue
tcp chat application
Issue with simple multi-threaded Echo server and client program
Question as follows
More...