sir i am trying to read msg for server and write to server,i can write
to server socket but when server socket write the client socket the
first msg write by server,can't read from client but the second msg is
read correctly.so kindly help me

here is the code
server socket***
***********
SocketThrdServe.java
*******************************************************
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Scanner;

class ClientWorker implements Runnable {
        private Socket client;

        ClientWorker(Socket client) {
                this.client = client;

        }

        public void run() {
                String line;
                BufferedReader in = null;
                PrintWriter out = null;

                try {
                        out = new PrintWriter(client.getOutputStream(), true);
                        ReadFromMob w1;
                        //out.println("hello client");
                        w1 = new ReadFromMob(client);

                        Thread t1 = new Thread(w1);
                        t1.start();

                } catch (IOException e) {
                        System.out.println("in or out failed");
                        System.exit(-1);
                }
                while (true) {

                        try {


                                try {

                                        // here the code connect the to the 
database and fetch the
                                        // upadte
                                        Class.forName("com.mysql.jdbc.Driver");
                                        Connection con = 
DriverManager.getConnection(
                                                        
"jdbc:mysql://localhost:3307/mac", "root", "sahil");
                                        Statement st = con.createStatement();
                                        ResultSet rs = st
                                                        .executeQuery("select 
id from 1rep_abhi limit 0,1");
                                        rs.next();
                                        // System.out.println("the data is:" + 
rs.getString(1));
                                        System.out.println("Plz enter the msg 
to send :");
                                        out.println("the data is:"
                                                        + new 
Scanner(System.in).nextLine());
                                        // out.println("From server:CNT" + cnt);

                                } catch (ClassNotFoundException e) {
                                        System.out.println("The error is:" + e);
                                } catch (SQLException e) {
                                        System.out.println("The error is:" + e);

                                }

                                /*
                                 * finally { rs=null; st=null; con=null; 
con.close(); }
                                 */


                        } catch (Exception e) {
                                System.out.println("Read failed" + e);
                                System.exit(-1);
                        }
                        out.flush();
                        }
        }
}

class ReadFromMob implements Runnable {
        // private BufferedReader in;
        private Socket client;
        BufferedReader in = null;

        ReadFromMob(Socket client) {
                this.client = client;

        }

        public void run() {
                try {
                        in = new BufferedReader(new InputStreamReader(
                                        client.getInputStream()));

                } catch (Exception e) {
                        System.out.println("the error is:" + e);
                }

                while (true) {
                        try {
                                System.out
                                                .println("The chiled Thread is 
waitin for incoming msg:");
                                System.out.println("The msg from client is:" + 
in.readLine());

                        } catch (IOException e) {
                                System.out.println(e);
                        }

                }
        }
}

class SocketThrdServer {

        ServerSocket server = null;

        SocketThrdServer() { // Begin Constructor

        } // End Constructor

        public void listenSocket() {
                try {
                        server = new ServerSocket(44446);
                        System.out.println("server listen on port 44446");
                } catch (IOException e) {
                        System.out.println("Could not listen on port 4444");
                        System.exit(-1);
                }
                while (true) {
                        ClientWorker w;
                        try {
                                w = new ClientWorker(server.accept());
                                System.out.println("accept a new client");
                                Thread t = new Thread(w);
                                System.out.println("create a new thread:" + 
t.getName());
                                t.start();
                        } catch (IOException e) {
                                System.out.println("Accept failed: 4444");
                                System.exit(-1);
                        }
                }
        }

        protected void finalize() {
                // Objects created in run method are finalized when
                // program terminates and thread exits

                try {
                        server.close();
                } catch (IOException e) {
                        System.out.println("Could not close socket");
                        System.exit(-1);
                }

        }

        public static void main(String[] args) {
                SocketThrdServer frame = new SocketThrdServer();

                frame.listenSocket();
        }

}
***************************
And here is the client socket

AndroidClientActivity.java
*************************
package com.android.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidClientActivity extends Activity {
        private EditText clientMsg;

        private Button connectPhones;

        private String serverIpAddress = "";

        private boolean connected = false;

        private Handler handler = new Handler();
        PrintWriter out;
        BufferedReader in;
        private TextView tv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                clientMsg = (EditText) findViewById(R.id.server_ip);

                connectPhones = (Button)
findViewById(com.android.client.R.id.connect_phones);
                new Thread(new ClientThread()).start();
                connectPhones.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                                out.println(clientMsg.getText());
                                out.flush();

                        }
                });
        }

        public class ClientThread implements Runnable {

                public void run() {
                        try {
                                Log.d("ClientActivity", "C: Connecting...");
                                Socket socket = new Socket("10.201.1.25", 
44446);
                                connected = true;
                                out = new PrintWriter(new BufferedWriter(
                                                new 
OutputStreamWriter(socket.getOutputStream())), true);
                                in = new BufferedReader(new InputStreamReader(
                                                socket.getInputStream()));
                                // tv=(TextView) 
findViewById(com.android.client.R.id.msg);
                                Log.d("ClientActivity", "C: Sending command.");

                                while (connected) {
                                        try {
                                                // read server msg

                                                if (in.readLine() == null) {
                                                        // 
Log.d("ClientActivity", "C: Sent."+in.readLine()

                                                } else {
                                                        Log.d("ClientActivity", 
"C: Sent." + in.readLine());

                                                }

                                        } catch (Exception e) {
                                                Log.e("ClientActivity", "S: 
Error", e);
                                        }
                                }
                                socket.close();
                                Log.d("ClientActivity", "C: Closed.");
                        } catch (Exception e) {
                                Log.e("ClientActivity", "C: Error", e);
                                connected = false;
                        }
                }

        }

}
**************************

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to