关于网络编程

m757266948 2017-03-20 06:54:56
客户端给服务端发送文本,服务端会将文本转成大写在返回给客户端。

public class TestTCPexer2 {

@Test
public void client() {

Socket socket = null;

OutputStream os = null;

BufferedReader br = null;

InputStream is = null;

try {
socket = new Socket(InetAddress.getByName("219.225.32.184"), 9090);
os = socket.getOutputStream();

System.out.println("请输入多个字符:");
InputStreamReader ir = new InputStreamReader(System.in);
br = new BufferedReader(ir);
String str;
while ((str = br.readLine()) != null) {
os.write(str.getBytes());
os.flush();
}
socket.shutdownOutput();

is = socket.getInputStream();
byte b[] = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

@Test
public void server() {

ServerSocket ss = null;

Socket s = null;

InputStream is = null;

OutputStream os = null;
try {
ss = new ServerSocket(9090);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[10];
int len;
String str = new String();
while ((len = is.read(b)) != -1) {
String str1 = new String(b, 0, len);
str += str1;
}

os = s.getOutputStream();
os.write(str.toUpperCase().getBytes());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}
}



控制台输入 abc 按回车没反应,哪里有问题呢(不想用Scanner 试试)
...全文
229 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeBron_song 2017-03-23
  • 打赏
  • 举报
回复
package com.practice;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author LeBron 作者 E-mail: 354962246@qq.com
 * @date 创建时间:2017年3月23日 下午7:44:25
 */
public class Client {
//客户端:
	public static void main(String[] args) throws Exception {
		Socket socket = new Socket("localhost", 8888);
		OutputStream os = socket.getOutputStream();
		InputStream is = System.in;
		BufferedInputStream brs = new BufferedInputStream(is);
		byte[] buf = new byte[1024];
		int res = 0;
		while ((res = brs.read(buf)) > -1) {
			os.write(buf);
		}

	}

}









//服务端:


package com.practice;

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

/** 
* @author LeBron 作者 E-mail: 354962246@qq.com
* @date 创建时间:2017年3月23日 下午7:59:18
*/
public class Server {

	public static void main(String[] args) throws Exception {
		ServerSocket serverSocket = new ServerSocket(8888);
		Socket ss = serverSocket.accept();
		InputStream is = ss.getInputStream();
		byte[] buf=new byte[1024];
		int res=0;
		while((res=is.read(buf))>-1){
			System.out.println(new String(buf,0,res).toUpperCase());
		}
		
		
		

	}

}
基本能实现你那个转大写的功能

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧