Results 1 to 4 of 4
Thread: Two simple programs interacting
- 12-28-2010, 06:57 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Two simple programs interacting
Hi!
I'm trying to make the code below work. But I don't know what I'm doing wrong as I dont get any error message. Some help would be much appriciated.
Java Code:package FleraProgram; import java.net.*; //Socket import java.io.*; //Strömmar,Utmatning/Inmatning //Detta program ansluter till ett väntande program och kommunicerar med det genom att //skicka heltal. class Anslutande { public static void main (String[] args) { Socket vantandePart = null; try { //Skapar förbindelsen till det väntande programmet i samma dator //som väntar på given port vantandePart = new Socket ("localhost",1201); //Kommunikationsverktyg OutputStream os = vantandePart.getOutputStream(); PrintWriter out = new PrintWriter (os,true); InputStream is = vantandePart.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); //Läser och sparar angivet heltal //out.println("Skriv in ett heltal:"); int u=2; int v; if(u==-1) { out.println("bla bla"); vantandePart.close(); } else { //Skicka heltalet out.println(u); //Ta emot svaret v=in.read(); //Visa svaret out.println(v); System.out.println(); } } catch (IOException e) { e.printStackTrace(); } finally { try { vantandePart.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Java Code:package FleraProgram; import java.net.*; //Socket,Serversocket import java.io.*; //Strömmar,Utmatning/Inmatning //Ett program som väntar på Anslutande.java. Vantande.java tar emot heltal och beräknar kvadrat //roten ur dessa och returnerar. class Vantande { public static void main(String[] args) { //väntande verktyg ServerSocket ss = null; //anslutande parten Socket anslutandePart = null; try { //objekt som väntar på given port ss = new ServerSocket(1201); //acceptera anslutningen anslutandePart = ss.accept(); //stäng väntande objekt ss.close(); //Kommunikationsverktyg OutputStream os = anslutandePart.getOutputStream(); PrintWriter out = new PrintWriter(os,true); InputStream is = anslutandePart.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); //ta emot heltal int v; v=in.read(); int u=(int) Math.sqrt(v); //visa heltal out.println(v); try { Thread.sleep(4000); } catch (InterruptedException e) {} //skicka svaret out.println(u); System.out.println(); } catch (IOException e) { e.printStackTrace(); } finally { try { ss.close(); anslutandePart.close(); } catch (IOException e) { e.printStackTrace(); } } } }Last edited by 1sprit; 12-28-2010 at 06:59 PM.
- 12-28-2010, 07:08 PM #2
What does it do wrong? Have you stepped through it with a debugger, or at least thrown in some print statements? What's happening? What did you expect to happen?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-28-2010, 07:56 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Anslutande.java sends an integer to Vantande.java. Vantande.java calculates the sqrt of the int and returns it. If Anslutande.java sends -1 the communication between the programs stops.
When i run it nothing happens. I have tried sending strings between them and that works.
- 12-29-2010, 01:16 AM #4
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 3
I would suggest you submit you code's comment in English instead of Swedish next time. As long as you have comments, share them, maybe people will be able to understand your code easier. If you don't what to translate them, remove them do the code gets clearer.
You can use å, ä, ö, α, β, γ, ψ, ζ or almost any other character in variable, method and class names, but if you do; make use you save the code file using UTF-8.
When generating Javadoc, I thing you may be required to add encoding=utf-8 as a parameter.
However I would not recommend using non-ASCII characters in Class names as they are used in file names which may cause problems is some scenarios.Last edited by Hibernate; 12-29-2010 at 01:21 AM.
Similar Threads
-
Interacting with a website
By eyeamchito in forum Advanced JavaReplies: 7Last Post: 05-29-2010, 11:00 PM -
Oracle trigger interacting with Java app
By KentDMc in forum Advanced JavaReplies: 1Last Post: 04-27-2009, 01:49 AM -
Interacting with the Java Garbage Collector
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:04 PM -
Interacting with files.
By apfroggy0408 in forum New To JavaReplies: 1Last Post: 03-19-2008, 06:14 AM -
I need a simple programs
By mikau in forum New To JavaReplies: 2Last Post: 02-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks