90 likes | 214 Views
CS 471/571. Sockets 2. UDP Client. from socket import * serverName = ‘ localhost ’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) message = raw_input( ’ Input lowercase sentence: ’ ) clientSocket.sendto (message,(serverName, serverPort))
E N D
CS 471/571 Sockets 2
UDP Client from socket import * serverName = ‘localhost’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage clientSocket.close()
UDP Server from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print “The server is ready to receive” while 1: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.upper() serverSocket.sendto(modifiedMessage, clientAddress)
TCP Client from socket import * serverName = ’localhost’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close()
TCP Server from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close()
Java UDP Client import java.io.*; import java.net.*; public class EchoClientUDP { public static void main(String[] args) throws Exception { DatagramSocket mySock = new DatagramSocket(); byte[] bytesToSend = args[0].getBytes(); InetAddress serverIP = InetAddress.getByName("localhost"); DatagramPacket sendPacket = new DatagramPacket(bytesToSend, bytesToSend.length, serverIP, 12000); DatagramPacket receivePacket = new DatagramPacket(new byte[bytesToSend.length] , bytesToSend.length); mySock.send(sendPacket); mySock.receive(receivePacket); System.out.println(new String(receivePacket.getData())); mySock.close(); } }
Java UDP Server import java.io.*; import java.net.*; public class EchoServerUDP { public static void main(String[] args) throws Exception { DatagramSocket mySock = new DatagramSocket(12000); DatagramPacket packet = new DatagramPacket(new byte[100], 100); mySock.receive(packet); String msg = new String(packet.getData()); msg = msg.toUpperCase(); packet.setData(msg.getBytes()); mySock.send(packet); mySock.close(); } }
Java TCP Client import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) throws Exception { Socket mySock = new Socket("localhost", 12000); BufferedReader in = new BufferedReader( new InputStreamReader(mySock.getInputStream())); PrintStream out = new PrintStream( mySock.getOutputStream()); String msg = args[0]; out.println(msg); System.out.println(in.readLine()); mySock.close(); } }
Java TCP Server import java.io.*; import java.net.*; public class EchoServer { public static void main(String[] args) throws Exception { ServerSocket mySock = new ServerSocket(12000); Socket client = mySock.accept(); BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream())); PrintStream out = new PrintStream( client.getOutputStream()); String msg = in.readLine(); msg = msg.toUpperCase(); out.println(msg); client.close(); mySock.close(); } }