黑马程序员技术交流社区
标题:
帮忙运行一下这个东西
[打印本页]
作者:
Super_Class
时间:
2013-6-6 19:12
标题:
帮忙运行一下这个东西
本帖最后由 Super_Class 于 2013-6-7 00:25 编辑
服务器端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo {
public static void main(String []args) throws IOException{
ServerSocket ss = new ServerSocket(8880);
Socket socket = ss.accept();
InputStream ips = socket.getInputStream();
OutputStream ops = socket.getOutputStream();
BufferedReader bufr = new BufferedReader(new InputStreamReader(ips));
String line = null;
while((line = bufr.readLine())!=null){
System.out.print(socket.getInetAddress().getHostName()+"::"+socket.getPort()+"::"+line);
}
ops.write("收到".getBytes());
System.out.println("发送成功");
ss.close();
socket.close();
}
}
复制代码
客户端
import java.io.IOException;
import java.io.*;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClientDemo {
public static void main(String []args) throws UnknownHostException, IOException{
Socket socket = new Socket("127.0.0.1",8880);
OutputStream ops = socket.getOutputStream();
ops.write("这个是TCP Client Demo".getBytes());
System.out.println("fasong成功");
InputStream ips = socket.getInputStream();
System.out.println("成功1");
BufferedReader bufr = new BufferedReader(new InputStreamReader(ips));
System.out.println("成功2");
String line = bufr.readLine();
System.out.println("成功3");
System.out.println("line = "+line);
System.out.println("成功4");
ops.close();
socket.close();
}
}
复制代码
结果一直在阻塞状态。求这个谁正在学这个的,看看。是什么情况。
作者:
刘海芳
时间:
2013-6-6 21:22
修改好的代码,错误地方,看注释
public class TCPServerDemo {
public static void main(String []args) throws IOException{
ServerSocket ss = new ServerSocket(8880);
Socket socket = ss.accept();
InputStream ips = socket.getInputStream();
OutputStream ops = socket.getOutputStream();
BufferedReader bufr = new BufferedReader(new InputStreamReader(ips));
// String line = null;
// while((line = bufr.readLine())!=null){//发过来的信息只有一行,所有不可以while循环
System.out.println(socket.getInetAddress().getHostName()+"::"+socket.getPort()+"::"+bufr.readLine());
// }
ops.write("收到 \r".getBytes());
System.out.println("发送成功");
ops.flush();
ss.close();
socket.close();
}
}
public class TCPClientDemo {
public static void main(String []args) throws UnknownHostException, IOException{
Socket socket = new Socket("127.0.0.1",8880);
OutputStream ops = socket.getOutputStream();
ops.write("这个是TCP Client Demo\n".getBytes());
System.out.println("fasong成功");
InputStream ips = socket.getInputStream();
System.out.println("成功1");
BufferedReader bufr = new BufferedReader(new InputStreamReader(ips));
System.out.println("成功2");
String line = bufr.readLine();
System.out.println("成功3");
System.out.println("line = "+line);
System.out.println("成功4");
ops.close();
socket.close();
}
}
复制代码
作者:
赵利斌
时间:
2013-6-6 21:34
肯定不是你指出的问题,就那点来说是不会影响代码的运行的,我也在看,难度很大啊
作者:
赵利斌
时间:
2013-6-6 22:33
import java.io.*;
import java.net.*;
public class TCPServerDemo {
public static void main(String []args) throws IOException, InterruptedException{
ServerSocket ss = new ServerSocket(1999);
Socket socket = ss.accept();//通过accept方法获取连接过来的客户端对象。
InputStream ips = socket.getInputStream();//获取客户端发送过来的数据,使用客户端对象的读取流来读取数据。
byte[] buf = new byte[1024];
int len = ips.read(buf);
System.out.println(new String(buf,0,len));
OutputStream ops = socket.getOutputStream();
System.out.println((socket.getInetAddress().getHostName()+"::"+socket.getPort()+"::"+new String(buf,0,len)));
Thread.sleep(10000);
ops.write("收到".getBytes());
System.out.println("发送成功");
socket.close();
ss.close();
}
}
[code]import java.net.*;
import java.io.*;
public class TCPClientDemo {
public static void main(String []args) throws IOException{
Socket s = new Socket("192.168.1.2",1999);
OutputStream out = s.getOutputStream(); //获得输出流
out.write("这个是TCP Client Demo".getBytes());
InputStream ips = s.getInputStream();
System.out.println("成功1");
byte[] bufIn = new byte[1024];
int num = ips.read(bufIn);
System.out.println(new String(bufIn,0,num));
System.out.println("成功2");
out.close();
s.close();
}
}
复制代码
[/code]
这个是可以的代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2