Results 1 to 4 of 4
  1. #1
    1sprit is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    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!

  3. #3
    1sprit is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    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.

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    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

  1. Interacting with a website
    By eyeamchito in forum Advanced Java
    Replies: 7
    Last Post: 05-29-2010, 11:00 PM
  2. Oracle trigger interacting with Java app
    By KentDMc in forum Advanced Java
    Replies: 1
    Last Post: 04-27-2009, 01:49 AM
  3. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:04 PM
  4. Interacting with files.
    By apfroggy0408 in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 06:14 AM
  5. I need a simple programs
    By mikau in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •