Socket编程二

客服端

 

ContractedBlock.gifExpandedBlockStart.gifCode
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;//使用IPAredd类,IPHostEntry类

namespace QQ
{
    
public partial class Tcpclient : Form
    {
        
private StreamWriter write;
        
private NetworkStream stream;
        
private TcpClient tcpclient;
        
public Tcpclient()
        {
            InitializeComponent();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
try
            {
                tcpclient 
= new TcpClient(AddressFamily.InterNetwork);
                tcpclient.Connect(
"127.0.0.1",8000);
                stream 
= tcpclient.GetStream();
                write 
= new StreamWriter(stream);
                write.WriteLine(
this.textBox1.Text);
                write.Flush();
            }
            
catch
            { 
            
            }
        }
    }
}

 

服务器端

 

ContractedBlock.gifExpandedBlockStart.gifCode
using System.Net.Sockets;//使用了TcpListen类
using System.Threading;//使用到了线程
using System.IO;//使用到了StreamReader类
using System.Net;

namespace QQ
{
    
public partial class Tcpserver : Form
    {
        
int port = 8000;//定义侦听端口号
        private Thread threadRead; //创建线程,用以侦听端口号,接受信息
        private TcpListener tcpListen;//侦听端口号 
        private bool blistener = true;
        
private NetworkStream stream;
        
private StreamReader read;
        
private TcpClient client;
        
private delegate void listbox_show(string str);
        listbox_show listboxshow;
        
public Tcpserver()
        {
            InitializeComponent();
            listboxshow 
= new listbox_show(show);
        }

        
private void Listen()
        {
            
try
            {
                tcpListen 
= new TcpListener(8000); //用8000端口来初始化TcpListener实例
                tcpListen.Start();//开始监听
                client = tcpListen.AcceptTcpClient();//通过tcp连接请求
                stream = client.GetStream();  //获取基本的流
                read = new StreamReader(stream); //用得到的网络基础数据流来初始化StreamReader实例
                while (blistener)
                {
                    
string message = read.ReadLine();//从网络基础数据流中读取一行数据
                    if (message == "STOP")
                    {
                        tcpListen.Stop();
//关闭侦听
                        stream.Close();//释放资源
                        read.Close();
                        threadRead.Abort();
                        
return;
                    }
                    
string sTime = DateTime.Now.ToShortDateString();
                    listBox1.Invoke(listboxshow,sTime 
+ " " + message);
                }

            }
            
catch (System.Security.SecurityException)
            {
                MessageBox.Show(
"侦听失败!","错误");
            }
        }
        
private void show(string str)
        {
            
this.listBox1.Items.Add(str);
        }
        
//开始侦听
        private void button1_Click(object sender, EventArgs e)
        {
            threadRead 
= new Thread(new ThreadStart(Listen));
            threadRead.Start();
            
this.button1.Enabled = false;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值