黑马程序员技术交流社区
标题:
关于客户端与服务器端传输数据之间的问题
[打印本页]
作者:
油炸电视剧
时间:
2015-6-6 23:09
标题:
关于客户端与服务器端传输数据之间的问题
就是客户端向服务器端发送一个字符串,服务器端向客户端返回字符串的大写。
下面是我的代码,就是服务器端不能返回大写字符串
客户端代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class TCPClient
{
public static void main(String[] args)
{
Socket s = null;
BufferedReader br = null;
BufferedWriter toServer = null;
BufferedReader fromServer = null;
try
{
s = new Socket("182.87.85.73", 5012);
br = new BufferedReader(new InputStreamReader(System.in));
toServer = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
fromServer = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
toServer.write(line);
toServer.newLine();
toServer.flush();
String str = fromServer.readLine();
System.out.println(str);
}
}
catch (IOException e1)
{
e1.printStackTrace();
}
finally
{
try
{
br.close();
s.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
复制代码
服务器端的代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer
{
public static void main(String[] args)
{
ServerSocket ss = null;
Socket s = null;
BufferedReader br = null;
BufferedWriter toClient = null;
try
{
ss = new ServerSocket(5012);
s = ss.accept();
String ip = s.getInetAddress().getHostName();
System.out.println(ip + "已连接");
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
toClient = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
String line;
while ((line = br.readLine()) != null)
{
toClient.write(line.toUpperCase());
toClient.newLine();
toClient.flush();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
ss.close();
s.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
复制代码
求大神指点。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2