TestTcp测试代码

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;
	}
}


PCAUSA Test TCP (PCATTCP) Release Notes Printing Communications Associates, Inc. 4201 Brunswick Court Smyrna, GA 30080 USA TEL: (770) 432-4580 FAX: (770) 436-7246 E-Mail: tdivine@pcausa.com ====================================================================== Component Part No.: N/A Component Name : PCAUSA Test TCP (PCATTCP) ====================================================================== Component Version : 2.01.01.11 Release Date : February 4, 2010 1.) Now flush stderr/stdout for all exit paths. ====================================================================== Component Version : 2.01.01.10 Release Date : November 3, 2009 1.) Added new -a option to specifiy local host IP address. 2.) Now built using Visual Studio 2008 (for better or worse...). ====================================================================== Component Version : 2.01.01.07 Release Date : November 23, 2003 1.) Incorporated fix identified by Clarkson University that reduces hangs when ending the UDP transmitter test. See additional comments in TTCP_TransmitUDP module. ====================================================================== Component Version : 2.01.01.06 Release Date : April 5, 2003 1.) Fixed minor bug in TTCP transmitter. PCAUSA PCATTCP preamble was not inserted correctly when building transmit buffer. ====================================================================== Component Version : 2.01.01.05 Release Date : May 31, 2002 1.) Modification to allow SO_RVCBUF and SO_SNDBUF values of zero(0) to be handled. Original implementation did not set these options if their value was zero. 2.) Added -w write delay option. ====================================================================== Component Version : 2.01.01.04 Release Date : May 30, 2002 1.) Minor fix to test exit routines. ====================================================================== Component Version : 2.01.01.03 Release Date : May 29, 2002 Externally Visible Differences ------------------------------ 1.) Added -c "continuous" option: -c -t: send continuously -r: accept multiple connections sequentially 1.) Added -R multi-threaded concurrent TCP/UDP receiver option. 2.) Fixed error codes. Now fetch using WSAGetLastError instead of perror. The latter did not handle socket errors. 3.) Revised test output messages. 4.) New PCATTCP.chm HtmlHelp documentation. Implementation Differences -------------------------- Extensive rework to suit author's style. ====================================================================== Component Version : 1.00.00.02 Release Date : January, 2000 Fix setting of setsockopt call for TCP_NODELAY. ====================================================================== Component Version : 1.00.00.01 Release Date : April, 1999 Initial release of PCAUSA's port of TTCP to Windows.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值