Socket编程一

[b][color=red]客户端启动后发给服务端一个信息,当服务端收到信息之后发给客户端一个反馈信息。。。[/color][/b]


[b]服务端:[/b]
package com.wlh.socket;
import java.io.*;
import java.net.*;
public class TestSockServer {
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
try {
ServerSocket ss = new ServerSocket(5888);
Socket socket = ss.accept();
in = socket.getInputStream();
out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
DataInputStream dis = new DataInputStream(in);
String s = null;
if((s=dis.readUTF())!=null) {
System.out.println(s);
System.out.println("from: "+socket.getInetAddress());
System.out.println("Port: "+socket.getPort());
}
dos.writeUTF("客户端你好......");
dis.close();
dos.close();
socket.close();
} catch (IOException e) {e.printStackTrace();}
}
}

[b]客户端:[/b]
package com.wlh.socket;
import java.net.*;
import java.io.*;
public class TestSockClient {
public static void main(String[] args) {
InputStream is = null; OutputStream os = null;
try {
Socket socket = new Socket("localhost",5888);
is = socket.getInputStream();
os = socket.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("服务端你好............");
String s = null;
if((s=dis.readUTF())!=null);
System.out.println(s);
dos.close();
dis.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {e.printStackTrace();}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值