当前位置:编程学习 > C#/ASP.NET >>

在同一台机器上能运行 在两太机器上不能运行 已经改过服务器ip地址了

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;
using System.Threading;

namespace 服务器端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket mysocket;
        Socket myclient;
        Thread th;
        private void Form1_Load(object sender, EventArgs e)
        {
            
            richTextBox1.ReadOnly = true;
            IPAddress ip = IPAddress.Parse("127.0.0.1");//可以随意更改任意一台服务器ip地址 
            IPEndPoint iep = new IPEndPoint(ip,5555);
            mysocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,ProtocolType.Tcp);
            try 
            {
                mysocket.Bind(iep);
                mysocket.Listen(2);
                myclient = mysocket.Accept();
                th = new Thread(new ThreadStart(ReadData));
                th.Start();
                

            }
            catch(Exception ex)
            {
                MessageBox.Show("服务器初始化失败"+ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myclient.Send(System.Text.Encoding.Unicode.GetBytes(textBox1.Text));
            richTextBox1.AppendText(DateTime.Now.ToShortTimeString()+"服务器对客户说:"+"\n"+textBox1.Text+"\r\n");
            textBox1.Clear();
            textBox1.Focus();
        }
        private void ReadData()
        { 
            while(true)
            {
                byte []buffer=new byte[1024];
                int k = myclient.Receive(buffer);
                string str = System.Text.Encoding.Unicode.GetString(buffer,0,k);
                if(str=="ClientClose")
                {
                    MessageBox.Show("客户端关闭,请关闭服务器");
                    myclient.Shutdown(SocketShutdown.Both);
                    myclient.Close();
                    mysocket.Close();
                }
                else
                    richTextBox1.AppendText(DateTime.Now.ToShortTimeString() + "客户对服务器说:" + "\n" + str + "\r\n");
            }
        }
        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                myclient.Send(System.Text.Encoding.Unicode.GetBytes("ServerClose"));
                th.Abort();
                myclient.Shutdown(SocketShutdown.Both);
                myclient.Close();
                mysocket.Close();
            }
            catch(Exception)
            {}
        }
    }
}
下面的这是客服端端设计
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;
using System.Threading;

namespace 客户端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Thread th;
        Socket mysocket;

        private void Form1_Load(object sender, EventArgs e)
        {
            IPAddress hostip = IPAddress.Parse("127.0.0.1");//必须和服务器端得ip地址一致
            IPEndPoint iep = new IPEndPoint(hostip, 5555);
            mysocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,ProtocolType.Tcp);
            try 
            {
                mysocket.Connect(iep);
                MessageBox.Show("连接服务器成功");
                th = new Thread(new ThreadStart(ReadData));
                th.Start();

            }
            catch(Exception ex)
            {
                MessageBox.Show("连接服务器失败"+ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mysocket.Send(System.Text.Encoding.Unicode.GetBytes(textBox1.Text));
            richTextBox1.AppendText(DateTime.Now.ToShortTimeString()+"客户对服务器说:"+"\n"+textBox1.Text+"\r\n");
            textBox1.Clear();
            textBox1.Focus();
        }
        private void ReadData()
        {
            while (true)
            { 
                byte []data=new byte[1024];
                int k = mysocket.Receive(data);
                string str = System.Text.Encoding.Unicode.GetString(data,0,k);
                if (str == "ServerClose")
                {
                    MessageBox.Show("服务器关闭,请关闭客户端");
                    mysocket.Shutdown(SocketShutdown.Both);
                    mysocket.Close();
                }
                else
                    richTextBox1.AppendText(DateTime.Now.ToShortTimeString() + "客户对服务器说:" + "\n" + str + "\r\n");
                   
            }

        }
        
    }
}


 在richTextBox1.AppendText(DateTime.Now.ToShortTimeString() + "客户对服务器说:" + "\n" + str + "\r\n")
这里提示错误!不知什么原因!
--------------------编程问答-------------------- 看你的程序是在函数中给控件赋值的
这个要用委托的
UI是一个线程,你的函数是另一个线程
你要更新UI的内容,只能让UI线程去做
--------------------编程问答--------------------
帮顶 --------------------编程问答-------------------- 晕了~~~啥东东啊 --------------------编程问答-------------------- 在坛子里搜搜跨线程访问控件 --------------------编程问答-------------------- 小弟 太菜 是在搞不懂这玩意 能否在我给的程序基础上 给修改一个 !! 谢谢!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,