Server and Client

启动时,先启动server,在启动client

Server

package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class server {
public static void main(String[] args) throws IOException {
ServerSocket s =new ServerSocket(8888);
Socket s1 = s.accept();
InputStream inp=s1.getInputStream();
OutputStream outp=s1.getOutputStream();
byte[] b=new byte[1024];
int len;
len =inp.read(b);
String str1=new String(b,0,len);
System.out.println(str1);
if(str1.equals("Hell Server")){
outp.write("Hello client".getBytes());
}
len =inp.read(b);
String str2=new String(b,0,len);
System.out.println(str2);
if(str2.equals("Bye")){
outp.write("Bye".getBytes());
s.close();
}
}
}

Client

package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s=new Socket("192.168.123.177",8888);
InputStream inp = s.getInputStream();
OutputStream outp = s.getOutputStream();
outp.write("Hell Server".getBytes());
byte[] b=new byte[1024];
int len=0;
len =inp.read(b);
String str1=new String(b,0 , len);
System.out.println(str1);
if (str1.equals("Hello client")) {
outp.write("Bye".getBytes());
}
len =inp.read(b);
String str2 = new String(b,0,len);
System.out.println(str2);
if(str2.equals("Bye")){
s.close();
}
}
}

*结束一定要关流

转载于:https://www.cnblogs.com/joyous-day/p/7794218.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值