java与c#的socket通信问题!求解答呀
本人做了个Java与C#进行Socket通信程序,使用java做了个客户端,C#做了个服务器端,两个程序进行Socket连接成功,客户端发送的数据服务器端可以收到,但服务器端返回数据时,客户端却收不到哦 ? 这是怎么回事啊 ?请高手指点
java:
import java.io.*;
import java.net.*;
public class myclass {
public static void main(String[] args)throws IOException
{
System.out.println("ready to test!\n");
run("127.0.0.1",33333);
}
public static void run(String hostip,int hostport)
{
try
{
Socket server = null;
String str = "this is amazing!";//null;
server = new Socket(hostip,hostport);
System.out.println("Connecting server\n");
/* OutputStream outstrm = server.getOutputStream();
DataOutputStream dos = new DataOutputStream(outstrm);
dos.writeUTF(str);
dos.flush();
dos.close();
*/
InputStream instrm = server.getInputStream();
DataInputStream dis =new DataInputStream(instrm);
System.out.println("utf !!!\n");
System.out.println(dis.readUTF());
dis.close();
server.close();
}
catch(IOException ex)
{
System.out.println(ex.toString());
}
}
}
c#:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
namespace GPSServer
{
class Program
{
private static Thread mythread;
// private static IPAddress serverIP = IPAddress.Parse("192.168.200.130");
private static IPEndPoint ipep;
private static Socket server;
static void Main(string[] args)
{
mythread = new Thread(new ThreadStart(BeginListen));
Console.WriteLine("监听");
mythread.Start();
Console.WriteLine("线程启动");
}
private static void BeginListen()
{
int recv;
ipep = new IPEndPoint(IPAddress.Any, 33333);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("已创建");
server.Bind(ipep);
byte[] data = new byte[1024];
while (true)
{
try
{
server.Listen(10);
Console.WriteLine("开始监听");
Socket client = server.Accept();
Console.WriteLine("为连接创建新socket完毕");
/* recv=client.Receive(data);
Console.WriteLine("接收完毕 "+recv);
string tem=Encoding.UTF8.GetString(data, 0, recv);
char[] tem2 = new char[recv];
// Console.WriteLine(tem.Length);
for (int i = 2; i < tem.Length; i++)
tem2[i-2] = tem[i ];
Console.WriteLine(tem2);
if ("quite" == Encoding.UTF8.GetString(data, 0, recv))
break;
//to be discussed!*/
Console.WriteLine("please enter a sentence:---");
string input = "hello world!";//Console.ReadLine();
client.Send(Encoding.ASCII.GetBytes(input));
Console.WriteLine("finished!\n");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
server.Close();
}
}
}