mono-project.de Community
 
Zurück   mono-project.de Community > Mono für Entwickler > ASP.NET, Webservices und Netzwerktechnologien

ASP.NET, Webservices und Netzwerktechnologien Themen rund um die ASP.NET Servertechnologie wie Webservices, Webcontrols, den XSP Webserver und Netzwerktechnologien wie TCP/UDP Sockets und Remoting

Antwort
 
Themen-Optionen Ansicht
  #1  
Alt 15.02.2010, 16:43:34
ZottiFX ZottiFX ist offline
Junior Member
 
Registriert seit: 15.02.2010
Beiträge: 1
Renommee-Modifikator: 0
ZottiFX is on a distinguished road
Standard socket auf dem mac

Hallo Leute,

wenn ich unten aufgeführten Code mit VisualStudio compiliere geht es unter windows.
Beim Mac kann ich keinen connect via telnet localhost 4444 herstellen.
Das Programm scheint zu arbeiten, aber funktionieren tut es nicht.
Hat jemand eine Ahnung, was ich da falsch mache?
Ich muss gestehen, bei mono bin ich erst seit ein paar Tagen.
Anbei der Code für die Consolenanwendung.

PHP-Code:

using System
;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TcpSock
{
    class 
TcpSock
    
{
        
int    tcpIndx 0;
        
int    tcpByte 0;
        
byte[] tcpRecv = new byte[1024];
        
        
////////////////////////////////////////
        
public Socket tcpSock;
        
////////////////////////////////////////
        
        
public int Recv  (ref string tcpRead)
        {
            
tcpByte tcpSock.Available;
            if (
tcpByte tcpRecv.Length tcpIndx)
            {
                
tcpByte tcpRecv.Length tcpIndx;
            }
            
tcpByte tcpSock.Receive(tcpRecvtcpIndxtcpByte,SocketFlags.Partial);
            
tcpRead Encoding.ASCII.GetString(tcpRecvtcpIndxtcpByte);
            
tcpIndx+= tcpByte;
            return    
tcpRead.Length;
        }
        
        public 
int RecvLn(ref string tcpRead)
        {
            
tcpRead Encoding.ASCII.GetString(tcpRecv0tcpIndx);
            
tcpIndx 0;
            return    
tcpRead.Length;
        }
        
        public 
int Send  (string tcpWrite)
        {
            return 
tcpSock.Send(Encoding.ASCII.GetBytes(tcpWrite));
        }
        
        public 
int SendLn(string tcpWrite)
        {
            return 
tcpSock.Send(Encoding.ASCII.GetBytes(tcpWrite "\r\n"));
        }
    }
        
        
    class 
Tcp
    
{
        [
STAThread]
        static 
void Main()
        {
            
////////////////////////////////////////////////////////////////////////////////////////////
            ///class IPHostEntry : Stores information about the Host and is required 
            ///for IPEndPoint.
            ///class IPEndPoint  : Stores information about the Host IP Address and 
            ///the Port number.
            ///class TcpSock     : Invokes the constructor and creates an instance.
            ///class ArrayList   : Stores a dynamic array of Client TcpSock objects.
        
            
IPHostEntry Iphe   Dns.Resolve   (Dns.GetHostName());
            
//IPHostEntry Iphe     = Dns.Resolve(Dns.BeginGetHostEntry('localhost'));
            
IPEndPoint  Ipep   = new IPEndPoint(Iphe.AddressList[0], 4444);
            
Socket      Server = new Socket    (Ipep.Address.AddressFamily
            
SocketType.StreamProtocolType.Tcp);
        
            
////////////////////////////////////////////////////////////////////////////////////////////
            ///Initialize
            ///Capacity : Maximux number of clients able to connect.
            ///Blocking : Determines if the Server TcpSock will stop code execution 
            ///to receive data from the Client TcpSock.
            ///Bind     : Binds the Server TcpSock to the Host IP Address and the Port Number.
            ///Listen   : Begin listening to the Port; it is now ready to accept connections.
        
            
ArrayList Client = new ArrayList ();
            
string    rln    null;
        
            
Client.Capacity =   256;
            
Server.Blocking false;
            
Server.Bind  (Ipep);
            
Server.Listen32 );
        
            
Console.WriteLine("{0}: listening to port {1}"Dns.GetHostName(), 
                
Ipep.Port);
        
            
////////////////////////////////////////////////////////////////////////////////////////////
            ///Main loop
            ///1. Poll the Server TcpSock; if true then accept the new connection.
            ///2. Poll the Client TcpSock; if true then receive data from Clients.
        
            
while (true)
            {
                
//Accept
                
if (Server.Poll(0SelectMode.SelectRead))
                {
                    
int i Client.Add(new TcpSock());
                    ((
TcpSock)Client[i]).tcpSock Server.Accept ();
                    ((
TcpSock)Client[i]).SendLn("Welcome.");
                    
Console.WriteLine("Client {0} connected."i  );
                }
        
                for (
int i 0Client.Counti++)
                {
                    
//check for incoming data
                    
if (((TcpSock)Client[i]).tcpSock.Poll(0SelectMode.SelectRead))
                    {
                        
//receive incoming data
                        
if (((TcpSock)Client[i]).Recv(ref rln) > 0)
                        {
                            
//echo incoming data
                            
((TcpSock)Client[i]).Send(rln);
        
                            
//check for new line
                            
if (rln == "\r\n")
                            {
                               
///receive line
                             
((TcpSock)Client[i]).RecvLn(ref rln);
        
                             
//send the line to all clients
                             
for (int y 0Client.County++)
                                if (
!= i)
                                ((
TcpSock)Client[y]).Send(i.ToString() + ": " rln);
        
                                 
Console.Write("{0}: {1}"irln);
                             }
                        }
                            
//recv returned <= 0; close the socket
                        
else
                        {
                            ((
TcpSock)Client[i]).tcpSock.Shutdown(SocketShutdown.Both);
                            ((
TcpSock)Client[i]).tcpSock.Close();
                            
Client.RemoveAt (i);
                            
Console.WriteLine("Client {0} disconnected."i);
                        }
                    }
                }
            }
        }
    }

Viele Grüße
Olli
Mit Zitat antworten
  #2  
Alt 15.02.2010, 18:22:10
ICEH ICEH ist offline
Senior Member
 
Registriert seit: 12.10.2005
Beiträge: 489
Renommee-Modifikator: 5
ICEH will become famous soon enough
Standard AW: socket auf dem mac

Schau mal mit netstat nach ob da überhaupt ein Port offen ist. (netstat -tuli)
Mit Zitat antworten
Antwort

Lesezeichen


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:55:29 Uhr.

Powered by vBulletin® Version 3.8.4 (Deutsch)
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.