客户端从键盘输入信息,发到服务端,服务端收到信息后,转成大写返回给客户端。

Server

package com.hbsi.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(3366);
Socket s=ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"......connection");
InputStream in = s.getInputStream();
BufferedReader brin=new BufferedReader(new InputStreamReader(in));
BufferedWriter bwout=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

String line=null;
while((line=brin.readLine())!=null){
if(line.equals("over"))
break;
System.out.println("Client:"+line);

bwout.write(line.toUpperCase());
bwout.newLine();
bwout.flush();
}
s.close();
ss.close();
}
}



Client



package com.hbsi.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class TCPClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.49.2", 3366);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bwout = new BufferedWriter(new OutputStreamWriter(s
.getOutputStream()));
BufferedReader brin = new BufferedReader(new InputStreamReader(s
.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
if(line.equals("over"))
break;
bwout.write(line);
bwout.newLine();
bwout.flush();
String str = brin.readLine();
System.out.println("server:" + str);
}
br.close();
s.close();
}
【客户端从键盘输入信息,发到服务端,服务端收到信息后,转成大写返回给客户端。】}

    推荐阅读