日期:2014-05-18 浏览次数:20690 次
package cn.dzr.tcp.tran;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TranService2
{
public static void main(String[] args) throws IOException
{
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"....connected!");
BufferedReader bufIn = new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line = bufIn.readLine())!= null)
{
System.out.println(line);
out.println(line.toUpperCase());
}
}
}
package cn.dzr.tcp.tran;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TranService
{
public static void main(String[] args) throws IOException
{
/*
* 1,建立服务端socket对象 ServerSocket
* 2,与客服端进行连接
* 3,读取客服端输入的字母
* 4,将字母改成大写,传回给客服端
*/
System.out.println("翻译服务端启动......");
ServerSocket ss = new ServerSocket(10003);
Socket s = ss.accept();
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
while(true)
{
byte[] buff = new byte[1024];
int len = in.read(buff);
String str = new String(buff,0,len);
if(str.equals("over"))
{
break;
}
System.out.println(str);
String str2 = str.toUpperCase();
out.write(str2.getBytes());
}
ss.close();
}
}