CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Sserver
{
public partial class Form1 : Form
{
private Socket m_mainSocket;
public Form1()
{
InitializeComponent();
textBoxIP.Text = GetIP();
FTServerCode.receivedPath = "";
}
String GetIP()
{
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Grab the first IP addresses
String IPStr = "";
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
IPStr = ipaddress.ToString();
return IPStr;
}
return IPStr;
}
public void StartListening()
{
//try
//{
// // Check the port value
// if (textBoxPort.Text == "")
// {
// MessageBox.Show("Please enter a Port Number");
// return;
// }
// string portStr = textBoxPort.Text;
// int port = System.Convert.ToInt32(portStr);
// // Create the listening socket...
// // tarife socket ke listen ro anjam mide
// m_mainSocket = new Socket(AddressFamily.InterNetwork,
// SocketType.Stream,
// ProtocolType.Tcp);
// // tarfie ip va noesh va port
// IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
// // Bind to local IP Address...
// //vasle kardane ip be socket
// m_mainSocket.Bind(ipLocal);
// // Start listening...
// // shoro kardane listen va in 4 yani teddade connectionhayee ke montazeer mimonan
// m_mainSocket.Listen(4);
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
private void Form1_Load(object sender, EventArgs e)
{
StartListening();
WebClient client = new WebClient();
// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
string baseurl = "http://www.whatismyip.org/";
Stream data = client.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString();
textBox1.Text = s;
}
class FTServerCode
{
IPEndPoint ipEnd;
Socket sock;
public FTServerCode()
{
ipEnd = new IPEndPoint(IPAddress.Any, 10000);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
}
public static string receivedPath;
public static string curMsg = "Stopped";
public void StartServer()
{
try
{
curMsg = "Starting...";
sock.Listen(100);
curMsg = "Running and waiting to receive file.";
Socket clientSock = sock.Accept();
byte[] clientData = new byte[1024 * 5000];
int receivedBytesLen = clientSock.Receive(clientData);
curMsg = "Receiving data...";
int fileNameLen = BitConverter.ToInt32(clientData, 0);
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + "/" + fileName, FileMode.Append));;
bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
curMsg = "Saving file...";
bWrite.Close();
clientSock.Close();
curMsg = "Reeived & Saved file; Server Stopped.";
}
catch
//(Exception ex)
{
//curMsg = "File Receving error.";
MessageBox.Show("File Receving error.");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
FTServerCode.receivedPath = fd.SelectedPath;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (FTServerCode.receivedPath.Length > 0)
{
backgroundWorker1.RunWorkerAsync();
button2.Enabled = false;
}
else
MessageBox.Show("Please select file receiving path","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
FTServerCode obj = new FTServerCode();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
obj.StartServer();
}
private void timer1_Tick(object sender, EventArgs e)
{
label5.Text = FTServerCode.receivedPath;
label3.Text = FTServerCode.curMsg;
}
}
}
This Code works in the same way as previous only this will only use list first IP and use this IP for listnening when (sending recieving files) How do I know which IP is then right one for listning when there may be several IP's and this Code only considers first IP.
CODE
String GetIP()
{
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Grab the first IP addresses
String IPStr = "";
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
IPStr = ipaddress.ToString();
return IPStr;
}
return IPStr;
This post has been edited by kingmighty_spades: 31 Mar, 2008 - 11:06 AM