黑马程序员技术交流社区
标题:
【记录】代码练习-TCP服务端给客户端反馈案例
[打印本页]
作者:
Kevin.Kang
时间:
2015-8-7 20:06
标题:
【记录】代码练习-TCP服务端给客户端反馈案例
服务器端:
package com.kxg_07;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(48264);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String str = new String(bys, 0, len);
OutputStream os = s.getOutputStream();
os.write("数据已收到".getBytes());
System.out.println(ip + ":" + str);
s.close();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-8-7 20:10
本帖最后由 Kevin.Kang 于 2015-8-7 20:14 编辑
客户端:
package com.kxg_07;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("10.164.22.254", 48264);
OutputStream os = s.getOutputStream();
os.write("hello,TCP".getBytes());
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String str = new String(bys, 0, len);
System.out.println("Server:" + str);
s.close();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2