| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 488 人关注过本帖
标题:求教~关于socket
只看楼主 加入收藏
Thriller_dd
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-3-13
收藏
 问题点数:0 回复次数:0 
求教~关于socket

想做一个可以广播的服务器
可是broadcast()有问题
求大虾指点一二~~不胜感激
源码如下:
using System;
using System.IO;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;

namespace ConsoleApplication1
{
class Program
{
private TcpListener tcp;
private Socket s;
public ArrayList connections=new ArrayList(1000);
private serveOne one, you;

public Program()
{
try
{
tcp = new TcpListener(8088);
tcp.Start();
Console.WriteLine("服务器启动~~~~");
while(true)
{
s = tcp.AcceptSocket();
one=new serveOne(s);
connections.Add(one);
}
}
catch(Exception e){}
}

public void broadcast()
{
for (int i = 0; i < connections.Count; i++)
{
you = (serveOne)connections[i];
you.write();
}
}

static void Main(string[] args)
{
Program my = new Program();
Thread th2 = new Thread(new ThreadStart(my.broadcast));
th2.Start();
}
}

class serveOne
{
private Socket socket;
private NetworkStream netWorkStream;
private String str="you connect...",str1 = "", str2;
private StreamReader sr;
private StreamWriter sw;

public serveOne(Socket s)
{
try
{
socket = s;
netWorkStream = new NetworkStream(socket);
sr = new StreamReader(netWorkStream);
sw = new StreamWriter(netWorkStream);

sw.WriteLine(str);
sw.Flush();

Thread th1 = new Thread(new ThreadStart(this.listen));
th1.Start();
}
catch(Exception e){}
}

public void listen()
{
try
{
while (true)
{
str1 = sr.ReadLine();
Console.WriteLine(str1);
}
}
catch (Exception e) { }
}

public void write()
{
try
{
while (true)
{
str2 = Console.ReadLine();
sw.WriteLine(str2);
sw.Flush();
}
}
catch (Exception e) { }
}
}
}

搜索更多相关主题的帖子: socket 
2006-03-13 13:53
快速回复:求教~关于socket
数据加载中...
 
   



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

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