using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;using System.Threading;namespace TCPChatServer{ class Program { public static TcpListener tcpListener; public static int port; public static IPAddress ip; public static TcpClient tcpClient; static void Main() { GetMyIP.get(); ip = GetMyIP.ip; ip = IPAddress.Parse("93.139.3.97"); port = 15555; tcpListener = new TcpListener(IPAddress.Any,port);
tcpListener.Start(); tcpClient = tcpListener.AcceptTcpClient(); NetworkStream netStream = tcpClient.GetStream(); Byte[] message = new Byte[1000]; int bytesRead; bytesRead = netStream.Read(message, 0, 100); string sms = ASCIIEncoding.ASCII.GetString(message); Console.WriteLine(sms); Console.WriteLine("end"); Console.Read(); } }}
and here's the "client":using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;using System.Threading;namespace SimpleChatClient{ class Program { static void Main() { int trace = 0; TcpClient client = new TcpClient(); // 93.139.3.97 192.168.1.4 IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("93.139.3.97"), 15555); Console.WriteLine(trace++); client.Connect(IPAddress.Parse("93.139.3.97"), 15555); Console.WriteLine(trace++); NetworkStream clientStream = client.GetStream(); Console.WriteLine(trace++); ASCIIEncoding encoder = new ASCIIEncoding(); byte[] buffer = encoder.GetBytes("Hello Server!"); Console.WriteLine(trace++); clientStream.Write(buffer, 0, buffer.Length); Console.WriteLine(trace++); clientStream.Flush(); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;using System.Threading;namespace SimpleChatClient{ class Program { static void Main() { int trace = 0; TcpClient client = new TcpClient(); // 93.139.3.97 192.168.1.4 IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("93.139.3.97"), 15555); Console.WriteLine(trace++); client.Connect(IPAddress.Parse("93.139.3.97"), 15555); Console.WriteLine(trace++); NetworkStream clientStream = client.GetStream(); Console.WriteLine(trace++); ASCIIEncoding encoder = new ASCIIEncoding(); byte[] buffer = encoder.GetBytes("Hello Server!"); Console.WriteLine(trace++); clientStream.Write(buffer, 0, buffer.Length); Console.WriteLine(trace++); clientStream.Flush(); } }}