求找出错误!

人生就这样 2017-02-05 02:36:00
package com.java.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import org.junit.Test;

public class socket {

@Test
public void 客户端(){
Socket socket=null;
OutputStream os=null;
Scanner scanner=null;
InputStream is=null;
try {
socket=new Socket(InetAddress.getByName("127.0.0.1"), 7032);
os= socket.getOutputStream();
System.out.print("请输入英文字母:");
scanner=new Scanner(System.in);
String str=scanner.next();
os.write(str.getBytes());

is= socket.getInputStream();
byte[] b=new byte[10];
int len;
while((len=is.read(b))!=-1){
String str1=new String(b, 0, len);
System.out.print(str1);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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(scanner!=null)
{
scanner.close();
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test
public void 服务端(){
ServerSocket ss=null;
Socket s=null;
InputStream is=null;
OutputStream os=null;
try {
ss=new ServerSocket(7032);
s=ss.accept();
is= s.getInputStream();

byte[] b=new byte[10];
int len;
String str = null;
while ((len=is.read(b))!=-1)
{
String str1=new String(b,0,len);
str+=str1;
}
String zb=str.toUpperCase();
os= s.getOutputStream();
os.write(zb.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is!=null)
{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os!=null)
{
try {
os.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();
}
}
}
}
}
...全文
279 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yexiongMYBH 2017-02-06
  • 打赏
  • 举报
回复
错误先贴出来再说哪里的问题,如果是连不上,应该要先启Server服务再能用client去连接
  • 打赏
  • 举报
回复
1、服务器端先开启,客户端连接服务器端 2、写完的时候要flush,冲刷到输出流中 3、while ((len = is.read(b)) != -1) { String str1 = new String(b, 0, len); str += str1; } 这段代码是阻塞的,后面的代码运行不了。
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SocketTest {
	public static void main(String[] args) {
		final SocketTest st = new SocketTest();
		new Thread(new Runnable(){

			@Override
			public void run() {
				st.服务端();
			}
			
		}).start();
		
		new Thread(new Runnable(){

			@Override
			public void run() {
				st.客户端();;
			}
			
		}).start();
	}

	public void 客户端() {
		Socket socket = null;
		OutputStream os = null;
		Scanner scanner = null;
		InputStream is = null;
		try {
			socket = new Socket(InetAddress.getByName("127.0.0.1"), 7032);
			os = socket.getOutputStream();
			System.out.print("请输入英文字母:");
			scanner = new Scanner(System.in);
			String str = scanner.next();
			os.write(str.getBytes());
			os.flush();

			is = socket.getInputStream();
			byte[] b = new byte[10];
			int len;
			while ((len = is.read(b)) != -1) {
				String str1 = new String(b, 0, len);
				System.out.print(str1);
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 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 (scanner != null) {
				scanner.close();
			}
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void 服务端() {
		ServerSocket ss = null;
		Socket s = null;
		InputStream is = null;
		OutputStream os = null;
		try {
			ss = new ServerSocket(7032);
			s = ss.accept();
			is = s.getInputStream();
			os = s.getOutputStream();

			byte[] b = new byte[10];
			int len;
			String str = null;
			while ((len = is.read(b)) != -1) {
				String str1 = new String(b, 0, len);
				str += str1;
				String zb = str.toUpperCase();
				os.write(zb.getBytes());
				os.flush();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (os != null) {
				try {
					os.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();
				}
			}
		}
	}
}
清风步月 2017-02-05
  • 打赏
  • 举报
回复
顺序啊。。先服务端监听端口,再客户端连接端口发送请求
csdn越来越坑 2017-02-05
  • 打赏
  • 举报
回复
报什么错啊?错误信息呢?
人生就这样 2017-02-05
  • 打赏
  • 举报
回复
为什么传换不了

62,620

社区成员

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

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