Results 1 to 2 of 2

Thread: socket closed

  1. #1
    moudhani is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Post socket closed

    HI

    nw i am coming with new thread regarding socket..below is my code.

    package com.nxp.atop;

    import java.io.BufferedReader;

    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.InetSocketAddress;
    import java.net.MalformedURLException;

    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketAddress;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.util.ArrayList;

    public class ServerSend
    {

    ServerSend()
    {

    }

    ArrayList parameterList=new ArrayList(40);
    public static URL url=null;
    public static URLConnection conn=null;
    public static OutputStreamWriter wr =null;

    //Method to open connection
    public int openconnection() throws IOException
    {
    int flag=0;
    try
    {
    url = new URL("http://49.249.140.243:8080/InsertIntoDataBase/InsertIntoTruck");
    // url = new URL("http://192.168.1.3:8080/InsertIntoDataBase/InsertIntoTruck");
    conn = url.openConnection();
    conn.setDoOutput(true);
    wr= new OutputStreamWriter(conn.getOutputStream());
    //System.out.println("Connection succuessful ");
    flag=1;
    }
    catch (Exception e)
    {
    e.printStackTrace();
    flag=0;
    }
    return flag;
    }



    //connection open closed//

    public void sendData(ArrayList paraneterList)
    {
    this.parameterList=paraneterList;
    System.out.println(parameterList);
    System.out.println("--------Sending 2 server running!!-----");
    System.out.println("The size is: "+parameterList.size());



    try {
    // Construct data
    String data = URLEncoder.encode("ACC_PEDAL", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(0), "UTF-8");
    data+= "&" + URLEncoder.encode("ENG_LOAD", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(1), "UTF-8");
    data += "&" + URLEncoder.encode("ENG_SPEED", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(2), "UTF-8");
    data += "&" + URLEncoder.encode("BARO_PRESSURE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(3), "UTF-8");
    data+= "&" + URLEncoder.encode("BOOST_PRESSURE_LB", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(4), "UTF-8");
    data += "&" + URLEncoder.encode("BOOST_PRESSURE_RB", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(5), "UTF-8");
    data += "&" + URLEncoder.encode("ENG_OIL_PRESSURE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(6), "UTF-8");
    data+= "&" + URLEncoder.encode("ENG_CRANKCASE_PRESSURE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(7), "UTF-8");
    data += "&" + URLEncoder.encode("ENG_COOLANT_PRESSURE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(8), "UTF-8");
    data += "&" + URLEncoder.encode("OIL_TEMP", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(9), "UTF-8");
    data+= "&" + URLEncoder.encode("COOLANT_TEMP", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(10), "UTF-8");
    data+= "&" + URLEncoder.encode("ENG_FUEL_RATE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(11), "UTF-8");
    data += "&" + URLEncoder.encode("ENG_TEMP_LB", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(12), "UTF-8");
    data+= "&" + URLEncoder.encode("ENG_TEMP_RB", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(13), "UTF-8");
    data += "&" + URLEncoder.encode("NORMAL_FRICTION_TORQUE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(14), "UTF-8");
    data+= "&" + URLEncoder.encode("ENG_FUEL_POS_LB", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(15), "UTF-8");
    data += "&" + URLEncoder.encode("ENG_FUEL_POS_RB", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(16), "UTF-8");
    data+= "&" + URLEncoder.encode("BATTERY_VOLTAGE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(17), "UTF-8");
    data += "&" + URLEncoder.encode("COMPRESSOR_TEMP", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(18), "UTF-8");
    data+= "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_LB1", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(19), "UTF-8");
    data += "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_LB2", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(20), "UTF-8");
    data+= "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_LB3", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(21), "UTF-8");
    data += "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_LB4", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(22), "UTF-8");
    data+= "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_LB5", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(23), "UTF-8");
    data += "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_LB6", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(24), "UTF-8");
    data+= "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_RB1", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(25), "UTF-8");
    data += "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_RB2", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(26), "UTF-8");
    data+= "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_RB3", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(27), "UTF-8");
    data += "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_RB4", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(28), "UTF-8");
    data+= "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_RB5", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(29), "UTF-8");
    data += "&" + URLEncoder.encode("EXHAUST_PORT_TEMP_RB6", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(30), "UTF-8");
    data += "&" + URLEncoder.encode("ENG_PRE_FILTER_OIL_PRESSURE", "UTF-8") + "=" + URLEncoder.encode(""+parameterList.get(31), "UTF-8");
    wr.write(data);
    wr.flush();
    }
    catch (Exception e) {
    System.out.println("Failed in Writing the data : " + e.getMessage());
    }
    BufferedReader rd = null;

    try {


    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String line;
    System.out.println("............The response is.................");
    while ((line = rd.readLine()) != null)
    {

    System.out.println(line);
    }
    System.out.println(".............................. ...................");
    System.out.println("Sending to server..");


    } catch (IOException e) {
    System.out.println("Failed in reading response from server : " + e.getMessage());
    }

    finally
    {
    try {
    rd.close();
    wr.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    // return flag;

    }


    }

    when i am executing this one..i am getting error "socket closed"..actually one month back it was working code.i dont know what is the problem(i think able to write data from outputstream and not able to read data from inputstream after opening URL connection)..friends can u help..its urgent..
    Last edited by moudhani; 11-24-2012 at 12:42 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: socket closed

    Moved from a staff-only section.

    Please go through the Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 02:14 AM
  2. Replies: 3
    Last Post: 04-05-2010, 02:19 AM
  3. Replies: 1
    Last Post: 01-31-2010, 03:17 AM
  4. Socket Closed!
    By rameshraj in forum Networking
    Replies: 0
    Last Post: 05-06-2008, 11:48 AM
  5. Socket Closed!
    By rameshraj in forum Networking
    Replies: 0
    Last Post: 05-06-2008, 10:11 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
  •