JAVAネットワークプログラミングUDP
5030 ワード
送信側
じゅしんたんし
クライアントはサービス側にテキストを送信し、サービス側はテキストを大文字に変換してクライアントに返す
サービス側
クライアント
package bank;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class send {
public static void main (String[] args){
DatagramSocket ds=null;
try {
ds = new DatagramSocket();
byte[] b = " , send".getBytes();
// , 64k, , ip, , up
DatagramPacket pack = new DatagramPacket(b,0,b.length, InetAddress.getByName("127.0.0.1"),9090);
ds.send(pack);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds!=null){
ds.close();
}
}
}
}
じゅしんたんし
package bank;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class send {
public static void main (String[] args){
DatagramSocket ds=null;
try {
ds = new DatagramSocket();
byte[] b = " , send".getBytes();
// , 64k, , ip, , up
DatagramPacket pack = new DatagramPacket(b,0,b.length, InetAddress.getByName("127.0.0.1"),9090);
ds.send(pack);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds!=null){
ds.close();
}
}
}
}
クライアントはサービス側にテキストを送信し、サービス側はテキストを大文字に変換してクライアントに返す
サービス側
package bank;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main (String[] args){
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;
}
String upperCase = str.toUpperCase();
os = s.getOutputStream();
os.write(upperCase.getBytes());
} catch (IOException e) {
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();
}
}
}
}
}
クライアント
package bank;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main (String[] args) {
Socket socket = null;
OutputStream os = null;
Scanner scanner = null;
//
InputStream is = null;
try {
//1. Socket , ip ,
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2. getOutputStream() , OutputStream
os=socket.getOutputStream();
//
System.out.println(" :");
scanner = new Scanner(System.in);
String str = scanner.next();
os.write(str.getBytes());
socket.shutdownOutput();
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 (IOException e) {
e.printStackTrace();
} finally {
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (scanner!=null){
scanner.close();
}
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}