黑马程序员技术交流社区
标题:
Java网络编程中的一点困惑,各位帮忙看看
[打印本页]
作者:
谢达
时间:
2013-3-7 21:20
标题:
Java网络编程中的一点困惑,各位帮忙看看
package com.ccsu.xie.net;
import java.io.*;
import java.net.*;
public class TcpDemo {
public static void main(String[] args) throws Exception {
//建立服务器端服务
ServerSocket ss = new ServerSocket(10000);
new Thread(new StringFormat(ss)).start();
//建立客户端Socket服务
Socket socket = new Socket("127.0.0.1",10000);
new Thread(new Writer(socket)).start();
}
}
//服务端:用来接收数据并转换(小写变大写) 再传给客户端
class StringFormat implements Runnable{
private ServerSocket ss = null;
public StringFormat(ServerSocket ss){
this.ss = ss;
}
@Override
public void run() {
/*
* 为什么放在whiel循环外面可以正常运行,
* Socket s=null;
try {
s = ss.accept();
} catch (Exception e) {
// TODO: handle exception
}
*/
while(true){
//放这里就不能正常运行了呢????????????????
Socket s=null;
try {
s = ss.accept();
} catch (Exception e) {
// TODO: handle exception
}
try {
System.out.println("服务器断正在等待数据.....");
System.out.println("Connected successed");
//获取输入流
InputStream is =s.getInputStream();
byte [] buf = new byte[1024];
int len = is.read(buf);
String info = new String(buf,0,len);
System.out.println("服务器端收到数据: "+info);
if("over".equals(info))
break;
//获取输出流
OutputStream os = s.getOutputStream();
info ="Server: "+ info;
os.write(info.toUpperCase().getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端:用来接收键盘输入数据,传给服务端,并接收服务端返回的信息
class Writer implements Runnable{
private Socket socket=null;
public Writer(Socket socket){
this.socket = socket;
}
@Override
public void run() {
OutputStream os=null;
InputStream is =null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入你想要转换的字符串");
String line = null;
try {
line = br.readLine();
System.out.println(line);
//获取socket输出流,并将键盘录入字符串发送到服务器端
os = socket.getOutputStream();
os.write(line.getBytes());
if("over".equals(line))
break;
//获取socket输入流
is = socket.getInputStream();
byte [] sInfo = new byte[1024];
int len = is.read(sInfo);
System.out.println("客户端收到数据: "+new String(sInfo,0,len));
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭socket流对象s
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
作者:
夏晓彤
时间:
2013-3-7 21:29
为我没看到问题
作者:
谢达
时间:
2013-3-8 09:00
夏晓彤 发表于 2013-3-7 21:29
为我没看到问题
//放这里就不能正常运行了呢????????????????
Socket socket = ss.accept();
放在while循环里面客户端就只能传输一次数据,之后就好像死锁了.
作者:
BitmapFactory
时间:
2013-3-8 11:13
本帖最后由 张学林 于 2013-3-8 11:16 编辑
第46行有说明,我把客户端单独拿出来的,以便验证
import java.io.*;
import java.net.*;
public class TcpDemo {
public static void main(String[] args) throws Exception {
//建立服务器端服务
ServerSocket ss = new ServerSocket(10000);
new Thread(new StringFormat(ss)).start();
//建立客户端Socket服务
// Socket socket = new Socket("127.0.0.1",10000);
// new Thread(new Writer(socket)).start();
}
}
//服务端:用来接收数据并转换(小写变大写) 再传给客户端
class StringFormat implements Runnable{
private ServerSocket ss = null;
public StringFormat(ServerSocket ss){
this.ss = ss;
}
@Override
public void run() {
// Socket s=null;
// try {
// s = ss.accept();
// } catch (Exception e) {
// // TODO: handle exception
// }
//
while(true){
//放这里就不能正常运行了呢????????????????
Socket s=null;
System.out.println("aaaaaaaaaaaafdsaf");
try {
s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"...."+s.getPort());
//这句话可以验证,当你把它放在里面的时候,s = null会把你的客户端端口关闭s = ss.accpet( )会
//重新开个接收的端口
} catch (Exception e) {
// TODO: handle exception
}
try {
System.out.println("服务器断正在等待数据.....");
System.out.println(s.getInetAddress().getHostAddress()+" Connected successed "+s.getPort());
//获取输入流
InputStream is =s.getInputStream();
byte [] buf = new byte[1024];
int len = is.read(buf);
String info = new String(buf,0,len);
System.out.println("服务器端收到数据: "+info);
if("over".equals(info))
break;
//获取输出流
OutputStream os = s.getOutputStream();
info ="Server: "+ info;
os.write(info.toUpperCase().getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端:用来接收键盘输入数据,传给服务端,并接收服务端返回的信息
class Writer /*implements Runnable*/{
public static void main(String[] args) {
Socket socket=null;
try {
socket = new Socket("127.0.0.1",10000);
System.out.println(socket.getPort()+"~~~~~~~~~~~~~~~~~");
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// private Socket socket=null;
// public Writer(Socket socket){
// this.socket = socket;
// }
// @Override
// public void run() {
OutputStream os=null;
InputStream is =null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入你想要转换的字符串");
String line = null;
try {
System.out.println("333333333333333");
line = br.readLine();
// System.out.println(line);
//获取socket输出流,并将键盘录入字符串发送到服务器端
os = socket.getOutputStream();
os.write(line.getBytes());
if("over".equals(line))
break;
//获取socket输入流
is = socket.getInputStream();
byte [] sInfo = new byte[1024];
int len = is.read(sInfo);
System.out.println("客户端收到数据: "+new String(sInfo,0,len));
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭socket流对象s
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
// }
}
}
复制代码
作者:
谢达
时间:
2013-3-18 22:20
张学林 发表于 2013-3-8 11:13
第46行有说明,我把客户端单独拿出来的,以便验证
对啊。。每次连接都重新获取socket对象。。应该可以正常运行
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2