using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace SUB5_Server
{
class SUB5_Server
{
const string strIP = "127.0.0.1";
const int BUF_SIZE = 4096;
const int PORT = 9090;
public static void StartListening(string path)
{
// Data buffer for incoming data.
byte[] bytes = new Byte[BUF_SIZE];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPAddress ipAddress = IPAddress.Parse(strIP);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
try
{
while (true)
{
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
NetworkStream ns = new NetworkStream(handler);
BinaryReader br = new BinaryReader(ns);
FileStream fs = null;
try
{
string filename;
while ((filename = br.ReadString()) != null)
{
int length = (int)br.ReadInt64();
fs = new FileStream(path+filename, FileMode.Create);
while (length > 0)
{
int nReadLen = br.Read(bytes, 0, Math.Min(BUF_SIZE, length));
//SaveFile(filename, buffer, nReadLen);
fs.Write(bytes, 0, nReadLen);
length -= nReadLen;
}
fs.Close();
}
}
catch (EndOfStreamException e)
{
//Console.WriteLine(e.ToString());
if (fs != null)
fs.Close();
}
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
static void Main(string[] args)
{
string path = "../../SERVER/";
Directory.CreateDirectory(path);
StartListening(path);
}
}
}
'프로그래밍 > C#' 카테고리의 다른 글
client 다른 버전 (0) | 2019.08.29 |
---|---|
C# 문자->숫자 (0) | 2019.03.24 |
C# 문자열->시간 (0) | 2019.03.24 |
C# 역문자열 (0) | 2019.03.24 |
C# 문자열, 숫자, 영어 검사 (0) | 2019.03.24 |