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
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Sockets and Internet Protocols
Getting a time out error
Jere Johnson
Greenhorn
Posts: 28
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello,
I am trying to create some code that will echo a message back from the
server. But I am getting a time out error. I need some help
in figuring out what to do. thanks.
import java.net.*; // for Socket import java.io.*; // for IOException public class TCPEchoClient { public static void main (String[] args) throws IOException { //create input stream InputStreamReader istream = new InputStreamReader(System.in); //create input stream buffer reader BufferedReader bufRead = new BufferedReader(istream); //if ((args.length < 2) || (args.length > 4)) //test for correct # of arguments //throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]"); //ask user to enter IP address or name, port number and message to be sent System.out.println("Enter the name or IP address of the server: "); String server = bufRead.readLine(); System.out.println("Enter Port number: "); String port = bufRead.readLine(); int servPort = Integer.parseInt (port); System.out.println("Enter the message to be sent: "); String message = bufRead.readLine(); // Message to be sent //convert input String to bytes using the default character encoding byte[] byteBuffer = message.getBytes(); //Create socket that is connected to server on specified port Socket socket = new Socket (server, servPort); System.out.println ("Connected to server.......sending echo string"); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); out.write(byteBuffer); //Send the encoded string to the server //Receive the same string back from the server int totalBytesRcvd = 0; int bytesRcvd; while (totalBytesRcvd < byteBuffer.length) { if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd, byteBuffer.length - totalBytesRcvd)) == -1) throw new SocketException("Connection closed prematurely"); totalBytesRcvd += bytesRcvd; } System.out.println ("Received: " + new String(byteBuffer)); socket.close(); //close the socket and its streams } }
Diapers are the best invention
jeff willis
Greenhorn
Posts: 25
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How well do you know the server?
Do you know that it is sending the data that you are expecting?
Did you write it? If so, make sure it is issuing a flush() call after the write. (same goes for you client).
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Java Server limit
Sending Serialized Objects to A Server And Displaying the contents
socket programming : connect server to client
My program has a bug. Need help
Can Someone tell me what I am doing wrong??
More...