.net套接字使用套接字进行数据传输(c#)

时间:2014-12-16 12:44:01

标签: c# android .net sockets networkstream

我试图在Android客户端和.net服务器之间传输一些数据。 我用套接字。客户端套接字连接在服务器中我没有得到响应。 任何人都可以查看此代码并帮助我?我有点失落。

我的客户代码:

Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            try 
            { 
                socket1.Connect(IPAddress.Parse(string_ip), 8080); 
                if (socket1.Connected) 
                { 
                    Log("connected");
                    NetworkStream networkStream1 = new NetworkStream(socket1);
                    byte[] sendData = new byte[1024];
                    byte[] recievedData = new byte[1024];
                    string message = "alon aviv";
                    sendData = Encoding.UTF8.GetBytes(message);
                    networkStream1.Write(sendData, 0, sendData.Length);
                } 
                else 
                { Log("not connected"); } 
            }

我的服务器代码:

Socket socket1 = new Socket(SocketType.Stream, ProtocolType.IP);
                System.Net.IPEndPoint ipEnd = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(string_ip), 8080);
                socket1.Bind(ipEnd);
                socket1.Listen(1);
                socket1.Accept();
                if (socket1.Connected)
                {
                    UpdateLogText("socket connected");
                    NetworkStream networkStream1 = new NetworkStream(socket1);
                    byte[] recievedData = new byte[1024];
                    networkStream1.Read(recievedData, 0, recievedData.Length);
                    string message_recieved = Encoding.ASCII.GetString(recievedData);
                    UpdateLogText(message_recieved);
                }

1 个答案:

答案 0 :(得分:1)

我也测试了你的代码,它也没有在我这边工作。我做了一些调整,确实有效。请尝试:

客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			byte[] bytes = new byte[1024];

			Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
			try
			{
				socket1.Connect(IPAddress.Parse("127.0.0.1"), 8080);
				if (socket1.Connected)
				{
					Console.WriteLine("Connected");



					// Encode the data string into a byte array.
					byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");

					// Send the data through the socket.
					int bytesSent = socket1.Send(msg);

					// Receive the response from the remote device.
					int bytesRec = socket1.Receive(bytes);
					Console.WriteLine("Echoed test = {0}",
						Encoding.ASCII.GetString(bytes, 0, bytesRec));

					// Release the socket.
					socket1.Shutdown(SocketShutdown.Both);
					socket1.Close();
				}
				else
				{
					Console.WriteLine("not connected");
				}

				Console.ReadLine();
			}
			catch (Exception e)
			{
				
			}
		}
	}
}

服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication2
{
	class Program
	{
		public static string data = null;

		static void Main(string[] args)
		{

			byte[] bytes = new Byte[1024];

			Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
			System.Net.IPEndPoint ipEnd = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8080);
			

			// Bind the socket to the local endpoint and 
			// listen for incoming connections.
			try
			{
				socket1.Bind(ipEnd);
				socket1.Listen(10);

				// Start listening for connections.
				while (true)
				{
					Console.WriteLine("Waiting for a connection...");
					// Program is suspended while waiting for an incoming connection.
					Socket handler = socket1.Accept();
					data = null;

					// An incoming connection needs to be processed.
					while (true)
					{
						bytes = new byte[1024];
						int bytesRec = handler.Receive(bytes);
						data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
						if (data.IndexOf("<EOF>") > -1)
						{
							break;
						}
					}

					// Show the data on the console.
					Console.WriteLine("Text received : {0}", data);

					// Echo the data back to the client.
					byte[] msg = Encoding.ASCII.GetBytes(data);

					handler.Send(msg);
					handler.Shutdown(SocketShutdown.Both);
					handler.Close();
				}

			}
			catch (Exception e)
			{
				Console.WriteLine(e.ToString());
			}

			Console.ReadLine();
		}
	}
}