Click here to Skip to main content
16,011,447 members
Home / Discussions / C#
   

C#

 
QuestionToolband with C#? Pin
22-Jan-02 15:32
suss22-Jan-02 15:32 
Questiontemplate classes?????? Pin
TheFLC21-Jan-02 22:00
TheFLC21-Jan-02 22:00 
AnswerRe: template classes?????? Pin
Joao Vaz22-Jan-02 4:31
Joao Vaz22-Jan-02 4:31 
AnswerRe: template classes?????? Pin
James T. Johnson22-Jan-02 4:41
James T. Johnson22-Jan-02 4:41 
GeneralRe: template classes?????? Pin
TheFLC22-Jan-02 20:28
TheFLC22-Jan-02 20:28 
GeneralRe: template classes?????? Pin
James T. Johnson22-Jan-02 21:15
James T. Johnson22-Jan-02 21:15 
AnswerRe: template classes?????? Pin
25-Jan-02 12:32
suss25-Jan-02 12:32 
Generalwho can tell me what's wrong with the code Pin
21-Jan-02 19:01
suss21-Jan-02 19:01 
the code is in the SDK,but wen i run it,nothing happened,except print "Connected xxx.xxx.xxx",it cann't receive message from the server,but the SDK say it can Receive Message From The Server,why??

The Address in SDK is:ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconnon-blockingclientsocketexample.htm

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;

// State object for receiving data from remote device.
public class StateObject {
public Socket workSocket = null; // Client socket.
public const int BufferSize = 256; // Size of receive buffer.
public byte[] buffer = new byte[BufferSize]; // Receive buffer.
public StringBuilder sb = new StringBuilder();// Received data string.
}

public class AsynchronousClient {
// The port number for the remote device.
private const int port = 21;

// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);

// The response from the remote device.
private static String response = String.Empty;

private static void StartClient() {
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// "host.contoso.com" is the name of the
// remote device.
IPHostEntry ipHostInfo = Dns.Resolve(Ftp.xxxx.com);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

// Connect to the remote endpoint.
client.BeginConnect( remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

// Send test data to the remote device.
Send(client,"USER xxx\r\n");
sendDone.WaitOne();

// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();

// Write the response to the console.
Console.WriteLine("Response received : {0}", response);

// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();

} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}

private static void ConnectCallback(IAsyncResult ar) {
try {
// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;

// Complete the connection.
client.EndConnect(ar);

Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());

// Signal that the connection has been made.
connectDone.Set();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}

private static void Receive(Socket client) {
try {
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;

// Begin receiving the data from the remote device.
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}

private static void ReceiveCallback( IAsyncResult ar ) {
try {
// Retrieve the state object and the client socket
// from the async state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;

// Read data from the remote device.
int bytesRead = client.EndReceive(ar);

if (bytesRead > 0) {
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));

// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}

private static void Send(Socket client, String data) {
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);

// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}

private static void SendCallback(IAsyncResult ar) {
try {
// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;

// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);

// Signal that all bytes have been sent.
sendDone.Set();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}

public static int Main(String[] args) {
StartClient();
return 0;
}
}


GeneralRe: who can tell me what's wrong with the code Pin
James T. Johnson21-Jan-02 20:06
James T. Johnson21-Jan-02 20:06 
Generalhandling WM_NOTIFY Pin
kasturirawat21-Jan-02 14:14
kasturirawat21-Jan-02 14:14 
GeneralGlobal instance Pin
Steve Severance21-Jan-02 12:50
Steve Severance21-Jan-02 12:50 
GeneralRe: Global instance Pin
James T. Johnson21-Jan-02 16:20
James T. Johnson21-Jan-02 16:20 
QuestionPrivate Assembly Security - how? Pin
Tariq21-Jan-02 8:46
Tariq21-Jan-02 8:46 
GeneralRole Based Security Pin
Sephrenia21-Jan-02 7:54
Sephrenia21-Jan-02 7:54 
GeneralFolder Events Pin
Sephrenia21-Jan-02 7:46
Sephrenia21-Jan-02 7:46 
GeneralRe: Folder Events Pin
James T. Johnson21-Jan-02 7:53
James T. Johnson21-Jan-02 7:53 
GeneralArrayList Pin
20-Jan-02 23:09
suss20-Jan-02 23:09 
GeneralRe: ArrayList Pin
James T. Johnson21-Jan-02 7:47
James T. Johnson21-Jan-02 7:47 
GeneralInvoking loaded types Pin
18-Jan-02 20:58
suss18-Jan-02 20:58 
GeneralRe: Invoking loaded types Pin
James T. Johnson19-Jan-02 18:25
James T. Johnson19-Jan-02 18:25 
GeneralLVN_ODCACHEHINT and LVN_ODFINDITEM Pin
kasturirawat18-Jan-02 12:28
kasturirawat18-Jan-02 12:28 
General.NET scanners Pin
Not Active18-Jan-02 7:36
mentorNot Active18-Jan-02 7:36 
GeneralRe: .NET scanners Pin
Peter Stephens18-Jan-02 17:58
Peter Stephens18-Jan-02 17:58 
QuestionHow to get windows directory? Pin
17-Jan-02 10:50
suss17-Jan-02 10:50 
AnswerRe: How to get windows directory? Pin
Navin17-Jan-02 11:03
Navin17-Jan-02 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.