【求助】获取IP问题...
首先这个不是代码的问题,不过我还是把代码粘贴下来。。。。我做的是一个C/S的类qq聊天的东西。。。
服务器端自动获取本地IP地址,利用socket,进行通行。
可是每次获取本地ip的时候,总是只能获取到127.0.0.1的ip
而我本机ip不可能是这个,127.0.0.1的回环地址。
而我肯定代码没有错误,因为我在别的机器上能够正确获取ip
在VM中也能获取到正确的ip。
我不知道是什么原因。
(以前同学在我机器上做网络实验,进行回环测试的实验。不知道是不是这个原因。)
总之,如果大家知道怎么解决,留个言,我感激不尽。。。。
粘贴代码,供学习交流。。。
服务器端:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using
using System.Threading;
namespace WindowsApplication2
{
public partial class qserver : Form
{
public qserver()
{
InitializeComponent();
}
private string ipv4;
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
ipv4 = ip[0].ToString();
this.label4.Text = ipv4;
}
TcpListener tcpls;
Socket sk;
NetworkStream ns;
StreamReader sr;
StreamWriter sw;
Thread tred;
private void button1_Click(object sender, EventArgs e)
{
tcpls = new TcpListener(1235);
tcpls.Start();
MessageBox.Show("服务器已启动");
this.button1.Enabled = false;
sk = tcpls.AcceptSocket();
MessageBox.Show("客户端已连接");
ns = new NetworkStream(sk);
sr = new StreamReader(ns, Encoding.UTF8);
sw = new StreamWriter(ns, Encoding.UTF8);
}
string smessage = "";
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Focus();
tred = new Thread(new ThreadStart(this.acceptcm));
tred.Start();
this.textBox2.Focus();
}
private void button3_Click(object sender, EventArgs e)
{
sendtoc();
this.textBox2.Focus();
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
sendtoc();
}
private void acceptcm()
{
lock (this)
{
try
{
if (sr.ReadLine() != null)
{
this.textBox1.Text += sr.ReadLine();
}
else
return;
}
catch (System.Exception ex)
{
}
finally
{
tred.Abort();
}
}
}
public void sendtoc()
{
try
{
smessage = "\r\n" + this.textBox3.Text + "(" + DateTime.Now.ToLongTimeString() + ")" + " " + this.textBox2.Text;
this.textBox1.Text += smessage;
this.textBox1.ScrollBars.
this.textBox2.Clear();
sw.WriteLine(smessage);
sw.Flush();
}
catch
{
MessageBox.Show("服务器端错误");
}
}
private void qserver_FormClosed(object sender, FormClosedEventArgs e)
{
if (tcpls != null)
tcpls.Stop();
//if (tred != null)
// tred.Abort();
Application.Exit();
}
}
}
客户端:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using
using System.Threading;
namespace Client
{
public partial class qclient : Form
{
public qclient()
{
InitializeComponent();
}
TcpClient tcpct;
NetworkStream ns;
StreamReader sr;
StreamWriter sw;
Thread trdc;
private void button1_Click(object sender, EventArgs e)
{
try
{
string ip = this.textBox4.Text + "." + this.textBox5.Text + "." + this.textBox6.Text + "." + this.textBox7.Text;
tcpct = new TcpClient(ip, 1235);
ns = tcpct.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
this.button1.Enabled = false;
}
catch
{
MessageBox.Show("链接服务器出错,请确认是否开启服务器...");
}
}
string cmessage = "";
private void button2_Click(object sender, EventArgs e)
{
try
{
sendtos();
this.textBox1.Text += cmessage;
}
catch
{
MessageBox.Show("出错啦!"+"\r\n"+"1.你是否已经连接服务器?"+"\r\n"+"2.等待服务器发回消息.");
}
finally
{
this.textBox2.Focus();
}
}
public void sendtos()
{
if (this.textBox2.Text == "") //发送
{
MessageBox.Show("内容不能为空");
return;
}
else
{
cmessage = "\r\n" + this.textBox3.Text + "(" + DateTime.Now.ToLongTimeString() + ")" + this.textBox2.Text;
this.textBox2.Clear();
// savechathistrory();
sw.WriteLine(cmessage);
sw.Flush();
}
}
public void acceptsm()
{
lock (this)
{
try //接收
{
this.textBox1.Text += sr.ReadLine() + "\r\n";
// savechathistrory();
}
catch
{
MessageBox.Show("出错啦!" + "\r\n" + "1.你是否已经连接服务器?" + "\r\n" + "2.没有服务器消息.");
}
finally
{
trdc.Abort();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Focus();
trdc = new Thread(new ThreadStart(acceptsm));
trdc.Start();
this.textBox2.Focus();
}
private void savechathistrory()
{
StreamWriter fsw = new StreamWriter(Application.UserAppDataPath + "chathistory.txt", false, Encoding.UTF8);
sw.WriteLine(this.textBox1.Text);
fsw.Flush();
fsw.Close();
}
private void qclient_FormClosed(object sender, FormClosedEventArgs e)
{
if (tcpct != null)
tcpct.Close();
Application.Exit();
}
private void button4_Click(object sender, EventArgs e)
{
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
sendtos();
}
private void qclient_Load(object sender, EventArgs e)
{
}
}
}
附窗体设计:
[local]2[/local]
[local]3[/local]