csharp_pain/ServerSocket1/Main.cs

76 lines
1.8 KiB
C#
Raw Permalink Normal View History

2014-06-26 15:13:46 +00:00
/*
* Created by SharpDevelop.
* User: windows
* Date: 18.11.2004
* Time: 16:58
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Net.Sockets;
using System.IO;
namespace ServerSocket1
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
try
{
bool status = true ;
string servermessage = "" ;
string clientmessage = "" ;
TcpListener tcpListener = new TcpListener(8100) ;
tcpListener.Start() ;
Console.WriteLine("Server Started") ;
Socket socketForClient = tcpListener.AcceptSocket() ;
Console.WriteLine("Client Connected");
NetworkStream networkStream = new NetworkStream(socketForClient) ;
StreamWriter streamwriter = new StreamWriter(networkStream) ;
StreamReader streamreader = new StreamReader(networkStream) ;
while(status)
{
if(socketForClient.Connected)
{
servermessage = streamreader.ReadLine() ;
Console.WriteLine("Client:"+servermessage) ;
if((servermessage== "bye" ))
{
status = false ;
streamreader.Close() ;
networkStream.Close() ;
streamwriter.Close() ;
return ;
}
Console.Write("Server:") ;
clientmessage = Console.ReadLine() ;
streamwriter.WriteLine(clientmessage) ;
streamwriter.Flush() ;
}
}
streamreader.Close() ;
networkStream.Close() ;
streamwriter.Close() ;
socketForClient.Close() ;
Console.WriteLine("Exiting") ;
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
}
}
}