16,022,362 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View .NET questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by mohamedhbmaam (Top 4 by date)
mohamedhbmaam
25-Feb-11 23:31pm
View
i have a project uses the webcam to send pictures repeatedly thats appear like a video
compress the pics by 7ZIP(open source) the receiver receive the img decompressed and print it
here the project http://www.megaupload.com/?d=T2UQNBJY hope this kind of help
mohamedhbmaam
25-Feb-11 15:27pm
View
this to enable the ping in LAN but the problem still exist
mohamedhbmaam
22-Feb-11 11:51am
View
no i have two pc when connect them locally (by switch) the ping command work fine but when i use different getway to each computer the ping command fail (time out error)
mohamedhbmaam
22-Feb-11 11:36am
View
Deleted
the problem is't in my code because my code work fine in LAN just like the ping(work fine in LAN too)
but here my code
TCPServer
<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace NetServer
{
public delegate object ClientConnectHandler(Socket datasocket);
public delegate void DataReceivedHandler(object obj,byte[] data);
public delegate void ClientDisconnectHandler(object obj);
public class TCPServer
{
public event ClientConnectHandler WhenClientConnect;
public event DataReceivedHandler WhenDataReceived;
public event ClientDisconnectHandler WhenClientDisconnect;
Socket ConnectionSocket;
IPEndPoint Endpoint;
byte[] Data=new byte[10000];
object obj=new object();
List<socket> OnlineClients = new List<socket>();
public TCPServer()
{
ConnectionSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
Endpoint = new IPEndPoint(IPAddress.Parse(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString()),6666);
}
public TCPServer(string ip, int port)
{
ConnectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Endpoint = new IPEndPoint(IPAddress.Parse(ip), port);
}
public void Listen()
{
ConnectionSocket.Bind(Endpoint);
ConnectionSocket.Listen(100);
ConnectionSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
public void OnClientConnect(IAsyncResult result)
{
Socket s = ConnectionSocket.EndAccept(result);
OnlineClients.Add(s);
if (WhenClientConnect != null)
obj = WhenClientConnect(s);
ConnectionSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
s.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), new SSocket { Object=obj , Socket=s});
}
public void OnDataReceived(IAsyncResult result)
{
SSocket ssocket = (SSocket)result.AsyncState;
try
{
int size = ssocket.Socket.EndReceive(result);
if (WhenDataReceived != null)
WhenDataReceived(ssocket.Object, Data.Take(size).ToArray());
Data = new byte[10000];
ssocket.Socket.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), new SSocket { Object = obj, Socket = ssocket.Socket });
}
catch(SocketException ex)
{
if(ex.ErrorCode == 10054)
{
foreach (Socket item in OnlineClients)
if (ssocket.Socket == item)
OnlineClients.Remove(item);
if (WhenClientDisconnect != null)
WhenClientDisconnect(ssocket.Object);
}
}
}
public void Send(Socket socket,byte[] data)
{
if(socket!=null)
socket.Send(data);
}
}
public class SSocket
{
object obj;
public object Object
{
get { return obj; }
set { obj = value; }
}
Socket socket;
public Socket Socket
{
get { return socket; }
set { socket = value; }
}
}
}
</pre>
TCPClient
<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
namespace NetClientTest
{
public delegate void DataReceivedHandler(byte[] data);
public class TCPClient
{
byte[] data = new b