//服务端程序
package tcpTest;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) {
Server s = new Server();
Receive rv = new Receive(s);
Thread trrv = new Thread(rv,"接收线程");
Send send = new Send(s);
Thread trsd = new Thread(send,"发送线程");
trsd.start();
trrv.start();
}
public synchronized void serverReceive(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
System.out.println("服务端接收");
try {
ss = new ServerSocket(9898);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[1024];
int len = 0 ;
while((len = is.read(b)) != -1){
System.out.println(new String(b ,0 ,len));
}
notifyAll();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
wait();
} catch (InterruptedException e) {
}
} //使用发送
public synchronized void serverSend(){
System.out.println("服务端发送"); ServerSocket ss = null;
Socket s = null;
OutputStream os = null;
try {
ss = new ServerSocket(9898);
s = ss.accept();
os = s.getOutputStream();
InputStream is = System.in;
byte [] b = new byte[1024];
int len = 0 ;
while( (len = is.read(b)) != -1){
os.write(b, 0, len);
break;
}
notifyAll();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
wait();
} catch (InterruptedException e) {
}
}
}
class Receive implements Runnable{ Server s ;
public Receive(){}
public Receive(Server s){
this.s = s;
}
@Override
public void run() {
while(true){
s.serverReceive();
}
}
}class Send implements Runnable{
Server s ;
public Send(){}
public Send(Server s){
this.s = s ;
}
@Override
public void run() {
while(true){
s.serverSend();
}
}
}
//客户端程序
package tcpTest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket; public class Client {
public static void main(String[] args) {
Client c = new Client();
ClientReceive rv = new ClientReceive(c);
Thread trrv = new Thread(rv,"接收线程");
ClientSend send = new ClientSend(c);
Thread trsd = new Thread(send,"发送线程");
trrv.start();
trsd.start();
}
public synchronized void clientReceive(){
Socket s = null;
InputStream is = null;
try {
s = new Socket(InetAddress.getByName("127.0.0.1"), 9898);
is = s.getInputStream();
byte[] b = new byte[1024];
int len = 0 ;
while((len = is.read(b)) != -1){
System.out.println(new String(b ,0 ,len));
System.out.println("客户端接收");
}
notify();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
wait();
} catch (InterruptedException e) {
}
} //使用发送
public synchronized void clientSend(){
Socket s = null;
OutputStream os = null;
try {
s = new Socket(InetAddress.getByName("127.0.0.1"), 9898);
os = s.getOutputStream();
InputStream is = System.in;
byte [] b = new byte[1024];
int len = 0 ;
while( (len = is.read(b)) != -1){
os.write(b, 0, len);
}
for (int i = 0; i < 4; i++) {
os.write("she".getBytes());
System.out.println("客户端发送");
}
notify();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
wait();
} catch (InterruptedException e) {
}
}
}
class ClientReceive implements Runnable{ Client c ;
public ClientReceive(){}
public ClientReceive(Client c){
this.c = c;
}
@Override
public void run() {
while(true){
c.clientReceive();
}
}
} class ClientSend implements Runnable{
Client c ;
public ClientSend(){}
public ClientSend(Client c){
this.c = c ;
}
@Override
public void run() {
while(true){
c.clientSend();
}
} }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货