客户端代码
public static void main(String[] args)
{
//读取命令行数据。
Socket socket;
try{
socket = new Socket(InetAddress.getLocalHost(),6767);
System.out.println(InetAddress.getLocalHost().toString());
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out=new PrintWriter(socket.getOutputStream());
out.write(args[0] + "," + args[1]);
out.flush();
System.out.println(1);
System.out.print(in.readLine());
socket.close();
}catch(IOException ex)
{
}
}服务器端代码
//读取命令行数据。
ServerSocket socket;
Socket client;
try{
socket = new ServerSocket(6767);
client = socket.accept();
System.out.println(client.getInetAddress().toString());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
System.out.println("1");
while(true)
{
String str = in.readLine();
System.out.println("begin receive....." + str);
out.println("has receive....." + str);
out.flush();
if (str == "end")
break;
}
client.close();
}catch(IOException ex)
{
}出现的结果:
客户端输出
本机电脑名称/ip地址
1
服务器端输出
/ip地址
1问题:
1、为什么我的客户端和服务器端连不上呢?
2、java的可以去连接c#的socket服务器吗?
谢谢
public static void main(String[] args)
{
//读取命令行数据。
Socket socket;
try{
socket = new Socket(InetAddress.getLocalHost(),6767);
System.out.println(InetAddress.getLocalHost().toString());
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out=new PrintWriter(socket.getOutputStream());
out.write(args[0] + "," + args[1]);
out.flush();
System.out.println(1);
System.out.print(in.readLine());
socket.close();
}catch(IOException ex)
{
}
}服务器端代码
//读取命令行数据。
ServerSocket socket;
Socket client;
try{
socket = new ServerSocket(6767);
client = socket.accept();
System.out.println(client.getInetAddress().toString());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
System.out.println("1");
while(true)
{
String str = in.readLine();
System.out.println("begin receive....." + str);
out.println("has receive....." + str);
out.flush();
if (str == "end")
break;
}
client.close();
}catch(IOException ex)
{
}出现的结果:
客户端输出
本机电脑名称/ip地址
1
服务器端输出
/ip地址
1问题:
1、为什么我的客户端和服务器端连不上呢?
2、java的可以去连接c#的socket服务器吗?
谢谢
try{
socket = new Socket(InetAddress.getLocalHost(),6767);
String s=InetAddress.getLocalHost().toString();
System.out.println(s);
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
String s=line.readLine();
while(true){
out.println(s);
out.flush();
if(s.equals("end"))break;
s=line.readLine();
continue;
}
out.write(s);
out.flush();
System.out.println(1);
System.out.print(in.readLine());
socket.close();
}catch(IOException ex) { }
还有out.write(args[0] + "," + args[1]); 有溢出,所以改为:
out.println(InetAddress.getLocalHost().toString());
我也是刚学,不知道能不能连c#的服务器
还有最后将异常输出也打印出来
服务器端其实不需要结束,只能能够读到客户发送的数据即可我想做个程序,在服务器端一直等待客户端发送数据的程序,客户端发完就自动关闭了。服务器一直在监听。to:solar2006
还有out.write(args[0] + "," +args[1]); 有溢出,所以改为:
===========================
那是因为我发送的内容是通过命令行参数传送的。to:jialin_2004
我去尝试一下输出异常,居然连这个都忘记了