Hallo ich habe mal wieder eine Frage, undzwar beschäftige ich mich mit der SocketAsynEventArgs Klasse, kriege soweit das Connecten, Listen und Receiven hin. Jedoch sobald ich mich an SendAsync dranmache klappt es nicht. Es passiert nichts und ich kann nichtmal prüfen wodran ich hängen geblieben bin. Ich zeig euch mal einfach meine 2 Klassen+Start:
[cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
namespace SocketTest
{
public class ClientSocket
{
ServerSocket Server = new ServerSocket();
public Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket Listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public IPEndPoint SilkroadClient = (new IPEndPoint(IPAddress.Parse("127.0.0.1"), 16000));
public void ListenForClient()
{
SocketAsyncEventArgs Args = new SocketAsyncEventArgs();
Args.Completed += new EventHandler<SocketAsyncEventArgs>(OnAccept);
Listener.Bind(SilkroadClient);
Listener.Listen(1);
Listener.AcceptAsync(Args);
}
private void OnAccept(object sender, SocketAsyncEventArgs e)
{
byte[] Buffer = new byte[8192];
e.SetBuffer(Buffer, 0, Buffer.Length);
e.Completed -= new EventHandler<SocketAsyncEventArgs>(OnAccept);
e.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
Socket = e.AcceptSocket;
Socket.ReceiveAsync(e);
}
private void OnReceive(object sender, SocketAsyncEventArgs e)
{
e.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
Socket.ReceiveAsync(e);
SocketAsyncEventArgs Args = new SocketAsyncEventArgs();
Args.SetBuffer(e.Buffer, e.Offset, e.BytesTransferred);
Server.Socket.SendAsync(Args);
}
}
}
[/cs]
[cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
namespace SocketTest
{
class ServerSocket
{
public Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public IPEndPoint SilkroadServer = (new IPEndPoint(IPAddress.Parse("212.24.57.34"), 15779));
public void ConnectToSilkroad()
{
SocketAsyncEventArgs Args = new SocketAsyncEventArgs();
Args.UserToken = Socket;
Args.RemoteEndPoint = SilkroadServer;
Args.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);
Socket.ConnectAsync(Args);
}
private void OnConnect(object sender, SocketAsyncEventArgs e)
{
byte[] Buffer = new byte[8192];
e.SetBuffer(Buffer, 0, Buffer.Length);
e.Completed -= new EventHandler<SocketAsyncEventArgs>(OnConnect);
e.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
Socket.ReceiveAsync(e);
}
private void OnReceive(object sender, SocketAsyncEventArgs e)
{
e.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
Socket.ReceiveAsync(e);
SocketAsyncEventArgs Args = new SocketAsyncEventArgs();
Args.SetBuffer(e.Buffer, e.Offset, e.BytesTransferred);
ClientSocket Client = new ClientSocket();
Client.Socket.SendAsync(Args);
}
}
}
[/cs]
[cs]
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.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace SocketTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ClientSocket Client = new ClientSocket();
ServerSocket Server = new ServerSocket();
private void button1_Click(object sender, EventArgs e)
{
Client.ListenForClient();
}
}
}
[/cs]