android 与 PC的socket通信

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class YaoChatServer extends Thread{

private YaoChatServer() throws IOException{
CreateSocket();
// 创建Socket服务器
}

public void run(){
Socketclient;
Stringtxt;
try {
while ( true )
// 线程无限循环,实时监听socket端口
{
client
= ResponseSocket();
// 响应客户端链接请求。。

while ( true )
{
txt
= ReceiveMsg(client);
System.out.println(txt);
// 链接获得客户端发来消息,并将其显示在Server端的屏幕上

SendMsg(client,txt);
// 向客户端返回消息

if ( true ) break ;
// 中断,继续等待链接请求
}

CloseSocket(client);
// 关闭此次链接
}
}
catch (IOExceptione){
System.out.println(e);
}

}

private ServerSocketserver = null ;
private static final int PORT = 5000 ;
private BufferedWriterwriter;
private BufferedReaderreader;

private void CreateSocket() throws IOException
{
server
= new ServerSocket(PORT, 100 );
System.out.println(
" Serverstarting.. " );
}

private SocketResponseSocket() throws IOException
{
Socketclient
= server.accept();
System.out.println(
" clientconnected.. " );

return client;
}

private void CloseSocket(Socketsocket) throws IOException
{
reader.close();
writer.close();
socket.close();
System.out.println(
" clientclosed.. " );
}

private void SendMsg(Socketsocket,StringMsg) throws IOException
{
writer
= new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
writer.write(Msg
+ " \n " );
writer.flush();

}

private StringReceiveMsg(Socketsocket) throws IOException
{
reader
= new BufferedReader(
new InputStreamReader(socket.getInputStream()));
System.out.println(
" servergetinputfromclientsocket.. " );
Stringtxt
= " Seversend: " + reader.readLine();

return txt;
}

public static void main( final Stringargs[]) throws IOException{
YaoChatServeryaochatserver
= new YaoChatServer();
if (yaochatserver != null ){
yaochatserver.start();
}
}

}

package com.android.Yao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.UnknownHostException;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget. * ;


public class YaoChatRoomAndroid extends Activity{
/** Calledwhentheactivityisfirstcreated. */
@Override
public void onCreate(BundlesavedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.main);


findviews();
setonclick();

}

private EditTextchattxt;
private TextViewchattxt2;
private Buttonchatok;

public void findviews()
{
chattxt
= (EditText) this .findViewById(R.id.chattxt);
chattxt2
= (TextView) this .findViewById(R.id.chattxt2);
chatok
= (Button) this .findViewById(R.id.chatOk);
}

private void setonclick()
{
chatok.setOnClickListener(
new View.OnClickListener(){

@Override
public void onClick(Viewv){
try {
connecttoserver(chattxt.getText().toString());
}
catch (UnknownHostExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
}
catch (IOExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
});
}


public void connecttoserver(StringsocketData) throws UnknownHostException,IOException
{
Socketsocket
= RequestSocket( " 192.168.10.119 " , 5000 );
SendMsg(socket,socketData);
Stringtxt
= ReceiveMsg(socket);
this .chattxt2.setText(txt);
}


private SocketRequestSocket(Stringhost, int port) throws UnknownHostException,IOException
{
Socketsocket
= new Socket(host,port);
return socket;
}

private void SendMsg(Socketsocket,Stringmsg) throws IOException
{
BufferedWriterwriter
= new BufferedWriter( new OutputStreamWriter(socket.getOutputStream()));
writer.write(msg.replace(
" \n " , " " ) + " \n " );
writer.flush();
}

private StringReceiveMsg(Socketsocket) throws IOException
{
BufferedReaderreader
= new BufferedReader( new InputStreamReader(socket.getInputStream()));

Stringtxt
= reader.readLine();
return txt;

}
}

<? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< TextView
android:id ="@+id/chattxt2"
android:layout_width
="319px"
android:layout_height
="68px"
android:text
="TextView"
android:layout_alignParentTop
="true"
android:layout_alignParentLeft
="true"
>
</ TextView >
< EditText
android:id ="@+id/chattxt"
android:layout_width
="319px"
android:layout_height
="52px"
android:text
="EditText"
android:textSize
="18sp"
android:layout_below
="@+id/widget30"
android:layout_alignParentLeft
="true"
>
</ EditText >
< Button
android:id ="@+id/chatOk"
android:layout_width
="320px"
android:layout_height
="41px"
android:text
="Button"
android:layout_below
="@+id/widget29"
android:layout_alignParentLeft
="true"
>
</ Button >

</ LinearLayout >

更多相关文章

  1. Android使用Retrofit进行网络请求
  2. haproxy根据客户端浏览器进行跳转
  3. Android(安卓)Paging组件Demo
  4. 【安卓笔记】android客户端与服务端交互的三种方式
  5. android手机客户端上传文件,java servlet服务器端接收并保存到服
  6. 一个简单的iPhone项目及代码
  7. 链接器解析多重定义的全局变量
  8. Android例子—直接通过Binder的onTransact完成跨进程通信
  9. Android(安卓)startActivityForResult的使用

随机推荐

  1. 如何解决恶意APK中常见的Native代码加壳
  2. Android:使用Handler在线程之间通信
  3. Android逆向之旅---静态分析技术来破解Ap
  4. android ios wp三大平台神器软件分享
  5. Android(安卓)数据存取试验 (一)
  6. android上传大文件亲测可用,上传200M个文
  7. Android(安卓)实现形态各异的双向侧滑菜
  8. 应用跳转(Deep Link)
  9. Android应届毕业生“过五关斩六将”,怒刷
  10. Android上架应用宝的那些坑