socket编程小问题

xiangle1993 2012-07-12 08:14:23
下面这个是server端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// 定义新的命名空间。
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;


namespace TalkServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 定义私有变量。
public bool m_bConnected = false;
public IPEndPoint p = null;
public Socket s = null;
public NetworkStream ns = null;
public TextReader r = null;
public TextWriter w = null;
public Thread t = null;
public void ThreadListen()
{
Socket recv = null;
recv = s.Accept();
if (recv != null)
m_bConnected = true;
ns = new NetworkStream(recv);
r = new StreamReader(ns);
w = new StreamWriter(ns);
while (m_bConnected)
{
string tmp;
try
{
tmp = r.ReadLine();
if (tmp.Length != 0)
{
lock (this)
{
richTextBox1.Text = "他说:" + tmp + "\n" + richTextBox1.Text;
}
}
}
catch
{
MessageBox.Show("连接断开!!");
}
}
recv.Shutdown(SocketShutdown.Both);
recv.Close();
s.Shutdown(SocketShutdown.Both);
s.Close();
}

private void button1_Click(object sender, EventArgs e)
{
p = new IPEndPoint(IPAddress.Any, 13002);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Blocking = true;
s.Bind(p);
s.Listen(0);
t = new Thread(new ThreadStart(this.ThreadListen));
t.Start();
button1.Enabled = false;
}

private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
{
byte[] data;
if (e.KeyChar == (char)13)
{
if (m_bConnected)
{
try
{
lock (this)
{
richTextBox1.Text = "我说:" + richTextBox2.Text + richTextBox1.Text;
w.WriteLine(richTextBox2.Text);
w.Flush();
}
}
catch
{
MessageBox.Show("连接断开!!");
}
}
else
{
MessageBox.Show("对不起,还没有与对方连接,不能通信!");
}
richTextBox2.Text = "";
richTextBox2.Focus();
}
}
}
}




另一个是client端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// 添加新的命名空间。
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TalkClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 定义私有变量。
public bool m_bConnected = false;
public Thread t = null;
public IPEndPoint p = null;
public Socket s = null;
public NetworkStream ns = null;
public TextReader r = null;
public TextWriter w = null;
public void ThreadListen()
{
string tmp;
while (m_bConnected)
{
try
{
tmp = r.ReadLine();
if (tmp.Length != 0)
{
lock (this)
{
richTextBox1.Text = "他说:" + tmp + "\n" + richTextBox1.Text;
}
}
}
catch
{
MessageBox.Show("连接断开!!");
}
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}

private void button1_Click(object sender, EventArgs e)
{

p = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(p);
if (s.Connected)
{
ns = new NetworkStream(s);
r = new StreamReader(ns);
w = new StreamWriter(ns);
t = new Thread(new ThreadStart(this.ThreadListen));
t.Start();
m_bConnected = true;
button1.Enabled = false;
}
}

private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
if (m_bConnected)
{
lock (this)
{
try
{
richTextBox1.Text = "我说:" + richTextBox2.Text + richTextBox1.Text;
w.WriteLine(richTextBox2.Text);
w.Flush();
}
catch
{
MessageBox.Show("连接断开!!");
}
}
}
else
{
MessageBox.Show("对不起,还没有与对方连接,不能通信!");
}
richTextBox2.Text = "";
richTextBox2.Focus();
}
}
}
}


这里的服务器地址和端口号都是由两个textbox获取,另有一个button按钮,用于连接服务器,但按下后出现了
“由于目标计算机积极拒绝,无法连接。 127.0.0.1:8888”de提示,我是初学的,请教各位了。

...全文
84 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
stonespace 2012-07-13
  • 打赏
  • 举报
回复
目标计算机积极拒绝,这个错误信息通常是因为服务器端防火墙造成的,关闭服务器端的防火墙和所有杀毒软件试试看,
nonocast 2012-07-13
  • 打赏
  • 举报
回复
改为localhost试试,嘿嘿
nonocast 2012-07-13
  • 打赏
  • 举报
回复
telnet 127.0.0.1 8888
测试服务器服务是否运行
zllqjf 2012-07-13
  • 打赏
  • 举报
回复

1.多线程问题,当你线程执行的时候,m_bConnected的值可能还是false,于是线程跳出循环直接关闭socket了;
2.防火墙的问题;

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧