C# Socket通信 客户端 服务器端代码

C/S结构的通信:

 

客户端:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace TcpClient
{
    public partial class Form1 : Form
    {
        public string serverIP = "127.0.0.1";
        public int serverPort = 8888;
        public IPAddress serverIPAddress;
        public System.Net.Sockets.TcpClient tcpClient;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serverIP = ipbox.Text;
            serverIPAddress = IPAddress.Parse(serverIP);
            serverPort = int.Parse(portbox.Text);
            tcpClient = new System.Net.Sockets.TcpClient();
            tcpClient.Connect(serverIPAddress,serverPort);

            if (tcpClient == null)
            {
                MessageBox.Show("无法连接到服务器,请重试!",
                                "错误",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
            }
            else
            {
                // 获取一个和服务器关联的网络流
                NetworkStream networkStream = tcpClient.GetStream();
                // 给服务器发送用户名
                byte[] userName_byte = Encoding.Unicode.GetBytes(userNameBox.Text.Trim());
                networkStream.Write(userName_byte,0,userName_byte.Length);
                networkStream.Flush();

                // 读取服务器返回的信息
                byte[] inforBuffer = new byte[100];
                networkStream.Read(inforBuffer,0,inforBuffer.Length);
                networkStream.Flush();
                string resultFromServer = Encoding.Unicode.GetString(inforBuffer);
                this.statusbox.Text = resultFromServer;
            }

        }
    }
}


 

 

 

服务器端:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace TcpServer
{
    class Listener
    {
        public TcpListener tcpListener;
        public int port = 8888;
        public IPAddress ipAddress = IPAddress.Parse("10.108.13.27");

        public void Start()
        {
            tcpListener = new TcpListener(ipAddress,port);
            tcpListener.Start();
            Console.WriteLine("begin listen port {0}",port);

            while (true)
            {
                byte[] buffer = new byte[100];

                Socket newClient = tcpListener.AcceptSocket();
                newClient.Receive(buffer);
                string userName = Encoding.Unicode.GetString(buffer).TrimEnd('\0');
                Console.WriteLine("user :{0} login",userName);

                newClient.Send(Encoding.Unicode.GetBytes("success"));

                Thread threadClient = new Thread(new ParameterizedThreadStart(clientProcess));
                threadClient.Start(newClient);
            }
        }

        public void clientProcess(object info)
        {
            Socket socket = info as Socket;
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值