Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: A single java program to receive and send plain text through differen ports.

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A single java program to receive and send plain text through differen ports.

    I have programmed a Router class which has two methods, receive and send.In receive method it receives the plain text from the server through port 2000.Its now all cool.In send method it sends the message to a client through the port 2001 but at the client i get an exception
     connection refused:connect
    import java.io.*;
    import java.util.*;
    import java.net.*;
    import java.sql.*;
    class Router
    {
    	String str;
    	public void receive()
    	{
    		try
    		{
    			while(true)
    			{
    				Socket so=new Socket("localhost",2000);
    				BufferedReader br=new BufferedReader(new InputStreamReader(so.getInputStream()));
    			 	str=br.readLine();
    				System.out.println("server has sent:"+str);
    				so.close();
    			}
     
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    	public void send()
    	{
    		int i,index,min=100;
    		int row=2;
    		try
    		{
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    			Connection cn=DriverManager.getConnection("jdbc:odbc:DSN2");
    			Statement st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			ResultSet rs=st.executeQuery("select * from Table1");
    			//rs.absolute(row);
    			rs.next();
    			rs.next();
    			//System.out.println(rs.getInt(1)+"\t"+rs.getInt(2)+"\t"+rs.getInt(3)+"\t"+rs.getInt(4)+"\t"+rs.getInt(5)+"\t"+rs.getInt(6));
    			for( i=2;i<7;i++)
    			{	
    				int value=rs.getInt(i);
    				System.out.println("hello");
    				if(value<min)
    				{
    					index=i;
    					min=value;
    				}
    			}
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    class Router1
    {
    	public static void main(String s[])
    	{
    		Router obj=new Router();
    		obj.receive();
    		obj.send();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A single java program to receive and send plain text through differen ports.

    I don't see a ServerSocket class in the posted code. Where is the code for the server?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A single java program to receive and send plain text through differen ports.

    Quote Originally Posted by Norm View Post
    I don't see a ServerSocket class in the posted code. Where is the code for the server?
    Yes I dint post the Server program.But i have programmed it correctly itself.I got the solution as the Client was trying to read from the Stream which is closed already in the server program.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A single java program to receive and send plain text through differen ports.

    the Stream which is closed already in the server program.
    That is why I asked to see the server code.

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. UDP Server Client program to send and receive messages
    By Koren3 in forum Java Networking
    Replies: 1
    Last Post: September 5th, 2011, 10:16 AM
  2. Send and Receive data from socket simultaneously
    By bigmac025 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 25th, 2011, 05:18 PM
  3. HELP!!!!!!! Send and receive between computers
    By dannyyy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2011, 05:16 PM
  4. Replies: 2
    Last Post: March 3rd, 2011, 02:22 PM
  5. [SOLVED] Send and Receive Parameter
    By java_mein in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: May 14th, 2010, 02:22 PM

Tags for this Thread