1 / 9

CS 471/571

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))

aliya
Télécharger la présentation

CS 471/571

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 471/571 Sockets 2

  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()

  3. 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)

  4. 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()

  5. 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()

  6. 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(); } }

  7. 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(); } }

  8. 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(); } }

  9. 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(); } }

More Related