package com.lovejia.TestTcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class TestTcpActivity extends Activity {
private Button btnLink;
private Button btnSend;
private EditText edt_ip;
private EditText edt_port;
private EditText edt_msg;
private EditText edt_showmsg;
Socket s;
BufferedReader in;
PrintWriter out;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt_ip = (EditText) findViewById(R.id.edt_ip);
edt_port = (EditText) findViewById(R.id.edt_port);
edt_showmsg = (EditText) findViewById(R.id.edt_Showmsg);
btnSend = (Button) findViewById(R.id.btnSend);
edt_msg = (EditText) findViewById(R.id.edt_msg);
btnLink = (Button) findViewById(R.id.btnLink);
btnLink.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(edt_ip.getText().equals(" ") || edt_port.getText().equals(" "))
{
showDialog(0);
return;
}
edt_showmsg.setText("服务器连接成功!\n");
// showDialog(1);
int port = 8596;//Integer.parseInt(edt_port.getText().toString());
InetAddress ip = null;
try {
//ip = InetAddress.getByName(edt_ip.getText().toString());
ip = InetAddress.getByName(null);
s = new Socket(ip,port);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
for(int i = 0; i < 10; i ++)
{
out.println("howdy " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btnLink.setEnabled(false);
edt_ip.setEnabled(false);
edt_port.setEnabled(false);
}
});
btnSend.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(edt_msg.getText().equals(""))
{
showDialog(0);
return;
}
String str = edt_msg.getText().toString();
edt_showmsg.append(str + "\n");
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
Dialog dialog = null;
Builder builder;
switch(id)
{
case 0:
builder = new AlertDialog.Builder(this);
builder.setTitle("对话框");
builder.setMessage("出错了");
dialog = builder.create();
break;
case 1:
builder = new AlertDialog.Builder(this);
builder.setTitle("对话框");
builder.setMessage("2出错了");
dialog = builder.create();
break;
default:
break;
}
return dialog;
// return super.onCreateDialog(id);
}
}
package com.lovejia.TestTcp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class WatchServer extends Thread{
Socket s;
BufferedReader in;
PrintWriter out;
public WatchServer(InetAddress ip,int port) throws IOException
{
s = new Socket(ip,port);
}
public void run()
{
while(true)
{
try{
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(),true);
if(in.readLine().equals(""))
{
}
}catch(IOException E)
{
System.out.println("发生异常"+E);
System.out.println("建立客户端联机失败!");
System.exit(2);
}
}
}
public void finalize()
{
try{
s.close();
}catch(IOException E){}
s = null;
}
}