24 กุมภาพันธ์ 2552

C# กับ Tcp

(Toggle Plain Text)
using System;

using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net;

namespace _8246_ACW_Coursework
{
class ServerApp
{
public void runServer()
{
//creates a TcpListener Object to listen for incoming connections
TcpListener listener;
//creates a socket to send and recieve data
Socket connection;
//creates a network stream
NetworkStream socketStream;

for (;;)
{
try
{
//creates an instance Tcp listenter to listen for connections on port 43
listener = new TcpListener(43);
//begins listening for incoming connections
listener.Start();
while (true)
{
//starts new thread
Thread thread = new Thread(runServer);
thread.Start();
//accepts socket if request is made to connect
connection = listener.AcceptSocket();
//sets a network stream instance to the connection socket
socketStream = new NetworkStream(connection);
//calls method doRequest which takes a socketstream and handles its request
doRequest(socketStream);
//closes the socket stream
socketStream.Close();
//closes connection
connection.Close();
}
}
catch (Exception e)
{
e.ToString();
}
}
}
public string doRequest(NetworkStream socketStream)
{
//streamReader reads incoming connection from client
StreamReader sr = new StreamReader(socketStream);
//writes in coming data
StreamWriter sw = new StreamWriter(socketStream);
//Assigns data from the streamreader to a string
string streamReader = sr.ReadLine();
return streamReader;
}
}
}


I have a client, which connects to the server.
using System;

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

namespace _8246_ACW_Coursework
{
public class ClientApp
{
//creates a new instance of the clientform windows form application
static ClientServerGUI clientform = new ClientServerGUI();

//variables
public static string address,
sendData,
updateData,
userName;
private static string space = " ";
public static int port;

//method which connects to the server and returns information back to the forms class
public static string SendData()
{
TcpClient client = new TcpClient();
//calls the TCP client and connects to the address and port passed in by the User from GUI
client.Connect(address, port);
//Creates a stream writer instance so user can send data to server
StreamWriter sw = new StreamWriter(client.GetStream());
//reads the information sent back from the server
StreamReader sr = new StreamReader(client.GetStream());

//if statement evalutes to see if the user has selected to update the server
//" " = update server
//"" = do not update the server
if(updateData.Equals(""))
{
space = "";
}
else if(!updateData.Equals(""))
{
space = " ";
}
//Refrences stream writer, username variable passed in from GUI
//space variable provides update function: "" = dont update. " " = update database.
sw.WriteLine(userName + space + updateData);
sw.Flush();
//data send back from the server assigned to string variable
string recieved = sr.ReadLine();
return recieved;

}
}
}

ไม่มีความคิดเห็น: