android pc socket,Android and PC Socket connection

I used the following code as Client side on android device

/*

* This is a simple Android mobile client

* This application read any string message typed on the text field and

* send it to the server when the Send button is pressed

*/

package lakj.comspace.simpleclient;

import java.io.IOException;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class SimpleClientActivity extends Activity {

private Socket client;

private PrintWriter printwriter;

private EditText textField;

private Button button;

private String messsage;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textField = (EditText) findViewById(R.id.editText1); //reference to the text field

button = (Button) findViewById(R.id.button1); //reference to the send button

// Button press event listener

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

messsage = textField.getText().toString(); //get the text message on the text field

textField.setText(""); //Reset the text field to blank

try {

client = new Socket("10.0.2.2", 4444); //connect to server

printwriter = new PrintWriter(client.getOutputStream(),true);

printwriter.write(messsage); //write the message to output stream

printwriter.flush();

printwriter.close();

client.close(); //closing the connection

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

});

}

}

and following as a server side as simple java project

/*

* This is a simple server application

* This server receive a string message from the Android mobile phone

* and show it on the console.

* Author by Lak J Comspace

*/

package simpleserver;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.ServerSocket;

import java.net.Socket;

public class Main {

private static ServerSocket serverSocket;

private static Socket clientSocket;

private static InputStreamReader inputStreamReader;

private static BufferedReader bufferedReader;

private static String message;

public static void main(String[] args) {

try {

serverSocket = new ServerSocket(4444); //Server socket

} catch (IOException e) {

System.out.println("Could not listen on port: 4444");

}

System.out.println("Server started. Listening to the port 4444");

while (true) {

try {

clientSocket = serverSocket.accept(); //accept the client connection

inputStreamReader = new InputStreamReader(clientSocket.getInputStream());

bufferedReader = new BufferedReader(inputStreamReader); //get client msg

message = bufferedReader.readLine();

System.out.println(message);

inputStreamReader.close();

clientSocket.close();

} catch (IOException ex) {

System.out.println("Problem in message reading");

}

}

}

}

i used simple button to send string from Android Emulator to java application but it's giving connection error.Which port and ip i should use instead of that mentioned in the code... and how to get that please help me

and how can i modify this code to send mobile contacts from android to PC??

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值