| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 500 人关注过本帖
标题:服务端与客户端
只看楼主 加入收藏
twtps
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-9-3
收藏
 问题点数:0 回复次数:1 
服务端与客户端
我写了这两个服务与客户端的程序,为什么老是出问题,有哪个高手帮忙解决一下
    服务端
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using .Sockets ;
using System.Threading;

namespace Receive__
{
    public partial class Form1 : Form
    {

        private IPAddress myIP;
        private IPEndPoint MyServer;
        private Socket sock;
        private bool bb;
        private Socket aaa;
        private Thread thread;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                myIP = IPAddress.Parse(textBox1.Text);
            }
            catch { MessageBox.Show("您输入的IP地址不正确"); }
            try
            {
                MyServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Bind(MyServer);
                sock.Listen(50);
                textBox3.Text = "主机" + textBox1.Text + "端口" + textBox2.Text + "开始监听...";
                thread = new Thread(new ThreadStart(targett));
                thread.Start();

            }
            catch (Exception ee) { textBox3.Text = ee.Message; }
        }

        private void targett()
        {
            aaa = sock.Accept();
            bb = false;
            if (aaa.Connected)
            {
                textBox3.Text = "与客户建立连接。";
                Byte[] bytee =new  Byte[64];
                bytee = System.Text.Encoding.BigEndianUnicode.GetBytes("您好,欢迎使用本服务器!".ToCharArray());
                aaa.Send(bytee, bytee.Length, 0);
                while (!bb)
                {
                    Byte[] bbb = new Byte[64];
                    aaa.Receive(bbb, bbb.Length, 0);
                    string ccc = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
                    richTextBox1.AppendText(ccc + "\r\n");
                    int len = richTextBox1.Lines.Length;
                    if (richTextBox1.Lines[len - 2] == "@@@@@")
                    {
                        textBox3.Text = "与客户断开连接.";
                        aaa.Shutdown();
                        aaa.Close();
                        bb = true;
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                bb = true;
                sock.Close();
                textBox3.Text = "主机" + textBox1.Text + "端口" + textBox2.Text + "停止监听";
            }
            catch { MessageBox.Show("监听尚未开始,关闭无效"); }
        }

    }
}




using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using
using System.Threading;

namespace 同步客户端
{
    public partial class Form1 : Form
    {
        private IPAddress myIP = IPAddress.Parse("127.0.0.1");
        private IPEndPoint MyServer;
        private Socket sock;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                myIP = IPAddress.Parse(textBox1.Text);
            }
            catch { MessageBox.Show("输入的IP不正确"); }
            try
            {
                MyServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect(MyServer);
                Thread thread=new Thread (new ThreadStart (targett));
                thread.Start ();
                textBox4.Text ="与主机"+textBox1.Text +"端口"+textBox2.Text +"连接成功!";
            }
            catch (Exception ee){MessageBox.Show (ee.Message );}
            
        }
        private void targett()
        {
            Byte[] bbb = new Byte[640];
            sock.Receive(bbb, bbb.Length, 0);
            string aaaaa = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
            textBox4.Text = aaaaa;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Byte[] bytee = new Byte[640];
            string send = textBox3.Text + "\r\n";
            bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
            sock.Send(bytee, bytee.Length, 0);
            Thread thread=new Thread (new ThreadStart (targett));
            thread.Start ();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Byte[] bytee = new Byte[64];
            string send = "@@@@@" + "\r\n";
            bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
            sock.Send(bytee, bytee.Length, 0);
            try
            {
                sock.Close();
                textBox4.Text = "与主机 " + textBox1.Text + "端口" + textBox2.Text + "断开连接!";
            }
            catch { MessageBox.Show("连接尚未建立,断开无效"); }
        }
    }
}
搜索更多相关主题的帖子: 客户端 服务端 
2009-10-04 16:22
vxbb
Rank: 6Rank: 6
等 级:侠之大者
帖 子:163
专家分:440
注 册:2008-8-18
收藏
得分:0 
直接把工程打包。方便调试

QQ:491697374  Mail:vxbb@
2009-10-04 18:28
快速回复:服务端与客户端
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036096 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved