java与c#的socket通信问题!求解答呀

EXLsunshine 2012-10-25 10:06:18
本人做了个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();
}

}
}
...全文
215 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
EXLsunshine 2013-04-21
  • 打赏
  • 举报
回复
引用 1 楼 byxxw 的回复:
我是同样的问题,求高手...
我当时解决了,貌似是android里面要加一句访问互联网的权限!你百度一下,应该能解决了!
byxxw 2013-02-19
  • 打赏
  • 举报
回复
我是同样的问题,求高手...

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧