java代码TCP/IP网络通信服务器客户端,实现双方信息交互。
3个回答
展开全部
package com.weixin.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 org.junit.Test;
public class ScoketTest {
@Test
public void client() throws Exception{
InetAddress i=InetAddress.getByName("127.0.0.1");
Socket s=new Socket(i, 9000);
OutputStream outputStream = s.getOutputStream();
outputStream.write("服务端你好,我是客户端哦!".getBytes());
s.shutdownOutput();
InputStream inputStream=s.getInputStream();
int length=0;
byte[] bytes=new byte[1024];
while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes,0,length));
}
inputStream.close();
outputStream.close();
s.close();
}
@Test
public void server() throws Exception{
ServerSocket serverSocket=new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
int length=0;
byte[] bytes=new byte[1024];
while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes, 0,length));
}
outputStream.write("客户端你好,本王已收到!".getBytes());
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
展开全部
package com.weixin.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 org.junit.Test;
public class ScoketTest {
@Test
public void client() throws Exception{
InetAddress i=InetAddress.getByName("127.0.0.1");
Socket s=new Socket(i, 9000);
OutputStream outputStream = s.getOutputStream();
outputStream.write("服务端你好,我是客户端哦!".getBytes());
s.shutdownOutput();
InputStream inputStream=s.getInputStream();
int length=0;
byte[] bytes=new byte[1024];
while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes,0,length));
}
inputStream.close();
outputStream.close();
s.close();
}
@Test
public void server() throws Exception{
ServerSocket serverSocket=new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
int length=0;
byte[] bytes=new byte[1024];
while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes, 0,length));
}
outputStream.write("客户端你好,本王已收到!".getBytes());
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
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 org.junit.Test;
public class ScoketTest {
@Test
public void client() throws Exception{
InetAddress i=InetAddress.getByName("127.0.0.1");
Socket s=new Socket(i, 9000);
OutputStream outputStream = s.getOutputStream();
outputStream.write("服务端你好,我是客户端哦!".getBytes());
s.shutdownOutput();
InputStream inputStream=s.getInputStream();
int length=0;
byte[] bytes=new byte[1024];
while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes,0,length));
}
inputStream.close();
outputStream.close();
s.close();
}
@Test
public void server() throws Exception{
ServerSocket serverSocket=new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
int length=0;
byte[] bytes=new byte[1024];
while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes, 0,length));
}
outputStream.write("客户端你好,本王已收到!".getBytes());
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个我可以做+我神兽3零74564零68
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询