18. april 2007 - 12:41Der er
14 kommentarer og 1 løsning
Find computers ip selv i console apply.
Jeg er ved at lave et program som jeg gerne vil havde skal finde en ip selv. intil nu har jeg måtte leve med selv at lave det om inde fra programmerings bruger fladen.
her er lidt mere kode så kan man skabe sig et overblik
try { // Register service on port 1234 serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(IPAddress.Parse("192.168.162.50"), 1234));
Console.WriteLine("Server bound to console"); serverSocket.Listen(0); //Wait for a connection from a client while (true) {
try { // Register service on port 1234 strHostName = DNS.GetHostName (); IPHostEntry ipEntry = DNS.GetHostByName (strHostName); IPAddress [] addr = ipEntry.AddressList; serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(addr[0], 1234)); // evt lige kig i addr og se om noget kan bruges først. Har ikke selv testet dette.
Console.WriteLine("Server bound to console"); serverSocket.Listen(0); //Wait for a connection from a client while (true) {
the name 'strHostName' does not exist in the current context the name 'DNS' does not exist in the current context the name 'DNS' does not exist in the current context the name 'strHostName' does not exist in the current context
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Collections;
public class Server { // Instance variable to hold names and addresses private Hashtable addressTable;
// main method to start the erver application public static void Main() { Server s = new Server(); s.Start(); }
public Server() { Console.WriteLine("Initializing Server data"); // Set up names and addresses addressTable = new Hashtable(10); addressTable.Add("Kasper", "d66"); addressTable.Add("Nick", "a1"); addressTable.Add("Martin", "kommer altid for sent"); addressTable.Add("Thomas", "a2"); }
Console.WriteLine("Starting server"); try { // Register service on port 1234 strHostName = DNS.GetHostName(); IPHostEntry ipEntry = DNS.GetHostByName(strHostName); IPAddress[] addr = ipEntry.AddressList; serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(addr[0], 1234)); // evt lige kig i addr og se om noget kan bruges først. Har ikke selv testet dette.
Console.WriteLine("Server bound to console"); serverSocket.Listen(0); //Wait for a connection from a client while (true) { //Wait here and listen for a connection Console.WriteLine("Waiting for client connection ..."); // Accepting a connection provides a socket to handle // client-server communications socket = serverSocket.Accept(); Console.WriteLine("Connection Accepted"); stream = new NetworkStream(socket); reader = new StreamReader(stream); writer = new StreamWriter(stream);
Console.WriteLine("Reading client data"); String queryString = reader.ReadLine(); Console.WriteLine("Client data is : {0} ", queryString);
// Now obtain information from addressTable // need to cast the result to a string as Object is returned // by default String result = (string)addressTable[queryString];
Console.WriteLine("Result is {0} ", result);
// Return information to client // Get a communications stream from the socket Console.WriteLine("Sending data to client"); writer.Write(result); writer.Flush(); Console.WriteLine("Data sent, closing connection");
Det vil jeg så lige prøve at grave lidt i og se hvad det kan :D
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.